一、pom文件导入依赖
注意版本,
hive-exec:可以参考hive家目录下的lib目录的jar文件版本,或者直接复制出来导入项目
hadoop-common:可以参考hadoop家目录下的share/hadoop/common目录的jar文件版本
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.4</version>
</dependency>
二、编写java代码
package hive.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
import org.apache.hadoop.io.IntWritable;
public class Maximum extends UDAF {
public static class MaximumIntUDAFEvaluator implements UDAFEvaluator{
private IntWritable result;
public void init() {
result = null;
}
public boolean iterate(IntWritable value) {
if (value == null){
return true;
}
if (result == null){
result = new IntWritable(value.get());
}else {
result.set(Math.max(result.get(), value.get()));
}
return true;
}
public IntWritable terminatePartial(){
return result;
}
public boolean merge(IntWritable orther){
return iterate(orther);
}
public IntWritable terminate(){
return result;
}
}
}
三、把项目打成jar包
(1)修改pom文件
加入:
<packaging>jar</packaging>
(2)打成jar包,右击package,点击Run Maven Build,然后会生成.jar文件
四、上传jar包,并添加到Hive
(1)上传jar包,并cd到该目录下
(2)执行命令:hive,进入hive,将jar文件添加到Hive
(3)用jar包生成函数
maximumttest:函数名,可以根据自己的情况取
hive.UDAF.Maximum:类所在的路径