具体步骤:
1、JAVA代码:
package com.bigdata;
import org.apache.hadoop.hive.ql.exec.UDF;
public class Add extends UDF {
public Integer evaluate(Integer a, Integer b) {
if (null == a || null == b) {
return null;
}
return a + b;
}
public Double evaluate(Double a, Double b) {
if (a == null || b == null) {
return null;
}
return a + b;
}
public Integer evaluate(Integer... a) {
int total = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != null) {
total += a[i];
}
}
return total;
}
}
2、将JAVA类打jar包并上传至hdfs中。
3、进入hive,执行命令CREATE FUNCTION default.add AS 'com.bigdata.Add' USING JAR 'hdfs://service/add.jar';