准备环境
1. 安装JDK
建议安装JDK 1.8以上版本
2. 安装开发工具
本文档测试使用IDEA工具,具体安装方式可以参考在线资料。
开发流程
1. 创建项目
创建MaxCompute Studio项目:在MaxCompute Studio项目下创建MaxCompute Java Module,如下所示
<?xml version="1.0"encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.aliyun.odps.myJava</groupId> <artifactId>MyFirstModel</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-core</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-udf</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-udf-local</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-mapred</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-mapred-local</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-graph</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-graph-local</artifactId> <version>${sdk.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28.odps</version> </dependency> </dependencies> <properties> <sdk.version>0.36.4-public</sdk.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project>
2. 开发UDF
注意在哪个目录下创建的类,注册的时候注意下类所在的位置)。为了区分,我们创建两个UDF类做验证对比学习,如下所示:
3. 本地测试
创建测试类,在本地测试,并验证结果符合预期,如下所示:
public class MyUDFTest { @Before public void start() { System.out.println("start"); } @After public void end() { System.out.println("end"); } @Test public void testMyUDF() { MyUDF myFirstUDF = new MyUDF(); System.out.println(myFirstUDF.evaluate("zhangsan")); } @Test public void testMyUDFCopy() { MyUDFCopy myUDFCopy = new MyUDFCopy(); System.out.println(myUDFCopy.evaluate("zhangsan")); } }
4. 本地打包
pom文件添加 <packaging>jarpackaging>, 双击执行Maven命令打jar包,如下所示:
打包后这里可以看到对应jar包,如下所示:
5. 上传资源并提交
登录Dataworks数据开发控制台,上传资源并提交(开发环境测试可以先不发布),如下所示:
6. 注册函数并提交
登录Dataworks数据开发控制台,创建函数并提交(开发环境测试可以先不发布)。测试验证写了两个类,注意注册配置的区别,如下所示:
7. 创建SQL节点执行验证
SELECT MYUDF("zhangsan"),MYUDFCOPY("zhangsan");
获取代码
由于测试的代码逻辑较为简单, 需要根据实际场景自行编辑逻辑,注意针对入参做下空值处理
import com.aliyun.odps.udf.UDF; public class MyUDF extends UDF { public String evaluate(String s) { return "hello"+ s; } }
package com.aliyun; import com.aliyun.odps.udf.UDF; public class MyUDFCopy extends UDF { public String evaluate(String s) { return "hi:"+ s; } }