1. 先建个maven工程,创建Java类,将你要实现的内容写进类的方法里,
例如public class StringExt extends UDF{
public String evaluate(String pnb){
return "Hello " + pnb;
}
}
这个类的实现很简单,继承了org.apache.hadoop.hive.ql.exec.UDF类,并覆写了evaluate方法
要注意写上
import org.apache.hadoop.hive.ql.exec.UDF;
同时在pom.xml里引入
<dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.1.0-cdh5.9.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.0-cdh5.9.0</version> </dependency>
要注意您引入的版本和服务器上的版本必须一致才行
2. 然后实现函数写完了就打包上传到服务器上(我是参考开发上传的路径,放到哪个路径下无所谓)
3. 这时就可以利用新的设置的函数了,我是直接将sql的执行写到文件里了,所以我直接在xxx.sql里写上我的函数
add jar file:///opt/program/xxx/xxx/xxx/xxxx.jar; create temporary function find as 'Utils.FinalConfirmUDF';(将你创建的类写在这里引入) SELECT find(xxxxx) FROM xxxxx (这个find函数就是我自定义写的) 然后执行hivesql文件 nohup hive -hiveconf date='init' -hivevar where='' -f xxx.sql > xxx.log &ds='${hiveconf:date} 你也可以直接在服务器上用命令行执行上述内容
参考文章:https://blog.youkuaiyun.com/l1028386804/article/details/78308907?locationNum=2&fps=1