本文以hive的upper函数为例,使用idea编程
1)创建一个Maven工程Hive
2)导入依赖(在pom.xml文件中插入)
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
3)创建一个类
package com.hanxiao.hive;
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.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
/*
函数说明:
select myupper('hive');
运行结果:HIVE
*/
public class MyUpper extends GenericUDF {
//初始化的方法:用了验证参数的个数,参数的类型、设置返回值的类型
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
//判断参数的个数
if (arguments.length !=1){
throw new UDFArgumentException(
"UPPER requires 1 argument, got"+arguments.length
);
}
//判断参数的类型
if (arguments[0].getCategory()!= ObjectInspector.Category.PRIMITIVE){
throw new UDFArgumentException(
"UPPER only takes primitive types, got"+arguments[0].getCategory()
);
}
//设置返回值的类型
return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
}
//逻辑方法
public Object evaluate(DeferredObject[] arguments) throws HiveException {
//获取传入的参数
Object param = arguments[0].get();
//将传入的参数转换为大写
return param.toString().toUpperCase();
}
public String getDisplayString(String[] children) {
return "This is my function";
}
}
4)打成jar包上传到服务器/opt/module/hive/datas/myupper.jar
5)将jar包添加到hive的classpath
hive (default)> add jar /opt/module/hive/datas/myupper.jar;
6)创建临时函数与开发好的java class关联
hive (default)> create temporary function myupper as "com.hanxiao.hive.MyUpper";
7)即可在hql中使用自定义的函数
hive (default)> show functions;
8)测试函数
hive (default)> select myupper('hanxiao');