UDF:一进一出
1、创建maven项目,导入依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
打包的时候可能会出现错误:
Could not transfer artifact org.pentaho:pentaho-aggdesigner-algorithm:pom:5.1.5-jhyde
解决方案: 在maven下的setting.xml文件中加入下面的镜像配置内容
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>spring-plugin</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
</mirror>
2、 编写代码
编写代码,继承org.apache.hadoop.hive.ql.exec.UDF,实现evaluate方法,在evaluate方法中实现自己的逻辑
package com.shujia;
import org.apache.hadoop.hive.ql.exec.UDF;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyUDF extends UDF {
public String evaluate(String col1) {
//2022/6/8-->2022-6-8
String[] split = col1.split("/");
String s = split[0] + "-" + split[1] + "-" + split[2];
return s;
}
}
3、打成jar包,上传Linux
4、在hive shell中,将jar包作为资源添加到hive环境中
add jar /usr/local/soft/jar/hadoop-hive-1.0-SNAPSHOT.jar;
使用jar包资源注册一个临时函数,fxxx1是你的函数名,'MyUDF'是主类名
create temporary function fxxx1 as 'MyUDF';
create temporary function date_change as 'com.shujia.MyUDF';
测试:
![]()