hive 支持自定义 UDF,UDTF,UDAF 函数
以自定义 UDF 为例:
使用一个名为 evaluate 的方法
package com.hive.custom;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;
public class XiaoUDF extends UDF {
/**
* 值加 1000
* @param i
* @return val
*/
public IntWritable evaluate(final IntWritable i) {
int val = i.get();
val += 1000;
return new IntWritable(val);
}
}
将写好的代码打为 jar 包, 上传到服务器, 或者 hdfs
add jar /root/udfxiao.jar;
//add jar you.jar
注册函数
注册一个临时函数
create temporary function fei as 'com.hive.custom.XiaoUDF';
//fei: 注册的函数名
//com.hive.custom.XiaoUDF 注册函数的全类名
使用函数
select fei(id) from test;
注册永久函数
create function testdb.peng as 'com.hive.custom.XiaoUDF';
//testdb 注册永久函数的数据库
从 HDFS 上注册函数
CREATE FUNCTION fei AS 'com.hive.custom.XiaoUDF' USING JAR 'hdfs:///udfxiao.jar';
// fei 注册的函数名
//com.hive.custom.XiaoUDF 函数的全内名
//hdfs:///udfxiao.jar hdfs 上根目录下的 jar
删除函数
drop temporary function if exists fei;
来源: http://www.bubuko.com/infodetail-2481322.html