一、为什么需要UDF函数
当Hive提供的内置函数无法满足你的业务处理需要时,此时就需要考虑使用用户自定义函数(UDF:user-defined function)。
二、UDF函数的分类
1.UDF(User-Defined-Function)
一进一出
2.UDAF(User-Defined Aggregation Function)
聚集函数,多进一出
类似于:count/max/min
3.UDTF(User-Defined Table-Generating Functions)
一进多出
如lateral view explore()
三、创建UDF函数
1.导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
2.创建类并继承org.apache.hadoop.hive.ql.UDF
import org.apache.hadoop.hive.ql.exec.UDF;
/**
* 转小写
*/
public class Lower extends UDF{
public String evaluate (final String s) {
if (s == null) {
return null;
}
return s.toLowerCase();
}
}
3.将项目打包成jar上传至服务器
4.在hive中添加jar资源
hive> add jar /getProduct_udf-1.0-SNAPSHOT.jar;
5.创建临时函数与jar中的udf函数类关联
hive> create temporary function mylower as "com.hive.udf.Lower";
6.调用自定义udf函数
hive> select mylower('HELLO WORLD');
OK
hello world
Time taken: 0.048 seconds, Fetched: 1 row(s)
注:
1.创建临时函数仅对当前session(黑窗口)有效
hive> create temporary function sayhello as 'com.ruozedata.bigdata.HelloUDF';
2.创建永久函数全局有效
hive> CREATE FUNCTION sayhello AS 'com.ruozedata.bigdata.HelloUDF' USING JAR 'hdfs://hadoop002:9000/lib/hive-1.0-SNAPSHOT.jar';