自定义函数
# 自定义函数
- UDF(User-Defined-Function)
一进一出
- UDAF(User-Defined Aggregation Function)
聚集函数,多进一出
类似于:count/max/min
- UDTF(User-Defined Table-Generating Functions)
一进多出
如 lateral view explore ()
https://cwiki.apache.org/confluence/display/Hive/HivePlugins
# 已过时都 UDF 方法
导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
继承 UDF 类 并书写 extends 方法
package com.atguigu.hive;
import org.apache.hadoop.hive.ql.exec.UDF;
public class MyUDF extends UDF {
//输入一个字符串 返回字符串长度 必须为evaluate这个方法名
public int evaluate(String input) {
if (input == null) {
return 0;
}
return input.length();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
打包成 jar, 再将 jar 包上传到 hive 中 /opt/module/hive/lib/
cd /opt/module/hive/lib/
1
在 hive 中添加 jar 包 或者 重启 hive 它会自动加载
add jar /opt/module/hive/lib/hive_function-1.0-SNAPSHOT.jar; -- 添加jar到classpath中
create temporary function my_len as "com.atguigu.hive.MyUDF";
-- create temporary function 自定义名称 as "自定义函数类路径"
1
2
3
2
3
使用时通过自定义的名称来使用
# 新 api
继承 GenericUDF 类 并重写抽象方法
package com.atguigu.hive;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
public class MyNewUDF extends GenericUDF {
/**
* 对输入的方法做检查 以及约束输出的类型
* @param objectInspectors 输入参数的检查器
* @return 输出的参数检查器
* @throws UDFArgumentException
*/
@Override
public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
//长度检查
if (objectInspectors.length !=1){
throw new UDFArgumentLengthException("Wrong arguments count!");
}
//类型检查
if (!objectInspectors[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)){
throw new UDFArgumentTypeException(0,"Wrong arguments type!");
}
return PrimitiveObjectInspectorFactory.javaIntObjectInspector; //返回java中int类型
}
/**
* 实现逻辑的方法
* @param deferredObjects
* @return
* @throws HiveException
*/
@Override
public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
Object o = deferredObjects[0].get();
if (o == null){
return 0;
}
return o.toString().length();
}
/**
* 函数执行出错 提示什么
* @param strings
* @return
*/
@Override
public String getDisplayString(String[] strings) {
return "";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
打包上传 hive lib 中
create temporary function my_len as "com.atguigu.hive.MyNewUDF";
1
使用
select ename, my_len(ename) from emp;
1
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48