package com.example.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public final class Lower extends UDF {
public Text evaluate(Text s) {
if (s == null) { return null; }
return new Text(s.toString().toLowerCase());
}
}
创建函数
加载jar包至hive
add jar /opt/dadas/hiveudf.jar
注册函数
create temporary function my_lower as 'com.example.hive.udf.Lower';
运行 hive> select my_lower(title), sum(freq) from titles group by my_lower(title);
注册函数的方法:
create temporary function my_lower as 'com.example.hive.udf.Lower'//临时函数
create function my_db.my_lower as 'com.example.hive.udf.Lower';
CREATE FUNCTION myfunc AS 'myclass' USING JAR 'hdfs:///path/to/jar';//必须保证jar在hdfs文件系统中
eg: CREATE FUNCTION mylower AS 'com.example.hive.udf.Lower' USING JAR 'hdfs://hdp-node-01:8020/user/root/hive/jar/hiveudf.jar