Hive基础知识(十九):Hive 自定义函数

发布于:2024-06-28 ⋅ 阅读:(14) ⋅ 点赞:(0)

1. 自定义函数

1)Hive 自带了一些函数,比如:max/min 等,但是数量有限,自己可以通过自定义 UDF 来方便的扩展。

2)当 Hive 提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UDF:user-defined function)。

3)根据用户自定义函数类别分为以下三种:

(1)UDF(User-Defined-Function)一进一出

(2)UDAF(User-Defined Aggregation Function)聚集函数,多进一出类似于:count/max/min

(3)UDTF(User-Defined Table-Generating Functions)一进多出如 lateral view explode()

4)官方文档地址 HivePlugins - Apache Hive - Apache Software Foundation

5)编程步骤:

(1)继承 Hive 提供的类

org.apache.hadoop.hive.ql.udf.generic.GenericUDF

org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;

(2)实现类中的抽象方法

(3)在 hive 的命令行窗口创建函数

添加 jar

add jar linux_jar_path 

创建 function

create [temporary] function [dbname.]function_name AS class_name; 

(4)在 hive 的命令行窗口删除函数

drop [temporary] function [if exists][dbname.]function_name;

2. 自定义 UDF 函数

0)需求:

自定义一个 UDF 实现计算给定字符串的长度,例如:

hive(default)> select my_len("abcd");
4

1)创建一个 Maven 工程 Hive

2)导入依赖

<dependencies>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>3.1.2</version>    
    </dependency>
</dependencies>

3)创建一个类

package com.zzz.udf;
​
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
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;
​
/**
*@author:左泽林
*@date:日期:2021-12-20-时间:16:28
*@message:
*/
public class MyUDF extends GenericUDF {
​
//校验数据参数个数
 public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
​
 if (arguments.length != 1){
 throw new UDFArgumentException("参数个数不为1");
}
​
 return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
}
​
//处理数据
 public Object evaluate(DeferredObject[] arguments) throws HiveException {
​
//1.取出输入数据
 String input = arguments[0].toString();
​
//2.判断输入数据是否为null
 if (input == null){
 return 0;
}
​
//3.返回输入数据的长度
 return input.length();
}
​
 public String getDisplayString(String[] strings){
 return "";
}
}

4)打成 jar 包上传到服务器/opt/module/data/myudf.jar

5)将 jar 包添加到 hive 的 classpath

hive (hive3)> add jar /home/root/hive/hive-demo-1.0-SNAPSHOT.jar;
Added [/home/root/hive/hive-demo-1.0-SNAPSHOT.jar] to class path
Added resources: [/home/root/hive/hive-demo-1.0-SNAPSHOT.jar]

6)创建临时函数与开发好的 java class 关联

hive (hive3)>  create temporary function my_len as "com.zzz.udf.MyUDF";
OK
Time taken: 0.104 seconds

7)即可在 hql 中使用自定义的函数

hive (hive3)> select my_len(name) from business;
FAILED: SemanticException [Error 10011]: Invalid function my_len
hive (hive3)> add jar /home/root/hive/hive-demo-1.0-SNAPSHOT.jar;
Added [/home/root/hive/hive-demo-1.0-SNAPSHOT.jar] to class path
Added resources: [/home/root/hive/hive-demo-1.0-SNAPSHOT.jar]
hive (hive3)>  create temporary function my_len as "com.zzz.udf.MyUDF";
OK
Time taken: 0.062 seconds
hive (hive3)> select my_len(name) from business;
OK
_c0
4
4
4
4
4
4
4
4
4
4
4
4
4
4
Time taken: 3.51 seconds, Fetched: 14 row(s)

3. 自定义 UDTF 函数

0)需求

自定义一个 UDTF 实现将一个任意分割符的字符串切割成独立的单词,例如:

hive(default)> select my_len("hello,world,hadoop,hive",",");
hello
world
hadoop
hive

1)代码实现

package com.zzz.udf;
​
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
​
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
​
/**
*@author:左泽林
*@date:日期:2021-12-20-时间:17:40
*@message:输入数据:hello,zzz,give
*输出:
* hello
* zzz
* give
*/
public class MyUDTF extends GenericUDTF {
​
//输出数据的集合
 private ArrayList<String> outPutList = new ArrayList<String>();
​
@Override
 public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
​
//输出数据的默认名,可以被别名覆盖
 List<String> fieldNames=  new ArrayList<String>();
 fieldNames.add("word");
​
//输出数据的类型
 List<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
 fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
​
//最终返回值
 return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs);
}
​
//处理输入数据的方法:hello,zzz,give
 public void process(Object[] args) throws HiveException {
​
//1.取出输出数据
 String input = args[0].toString();
​
//2.按照“,”分割字符串
 String[] words = input.split(",");
​
//3.遍历数据的写出
 for (String word : words){
//清空集合
 outPutList.clear();
//将数据放入集合
 outPutList.add(word);
//输出数据
 forward(outPutList);
}
}
​
//收尾方法
 public void close() throws HiveException {
​
}
}

2)打成 jar 包上传到服务器/opt/module/hive/data/myudtf.jar

3)将 jar 包添加到 hive 的 classpath 下

hive (default)> add jar /opt/module/hive/data/myudtf.jar;

4)创建临时函数与开发好的 java class 关联

hive (hive3)>  create temporary function my_len as "com.zzz.udf.MyUDTF";

5)使用自定义的函数

hive (hive3)> select my_len("hello,world,hadoop,hive");
OK
word
hello
world
hadoop
hive
Time taken: 3.614 seconds, Fetched: 4 row(s)