1、需求
自定义将字符串转大写和小写的Hive UDF;
2、maven依赖及代码
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.4</version>
</dependency>
</dependencies>
package com.hive;
import org.apache.hadoop.hive.ql.exec.UDF;
public class StrToUpperFunc extends UDF {
public String evaluate(String str){
//字符转化为大写
return str.toUpperCase();
}
}
package com.hive;
import org.apache.hadoop.hive.ql.exec.UDF;
public class StrToLowerFunc extends UDF {
//字符转化为小写
public String evaluate(String str){
return str.toLowerCase();
}
}
3、操作步骤
(1)将代码打包生成的hiveUdf.jar上传到hdfs的/udfFunc下;
(2)在hive端创建函数:
hive> create Function strToL as "com.hive.StrToLowerFunc" USING JAR 'hdfs:///udfFunc/hiveUdf.jar';
hive> create Function strToU as "com.hive.StrToUpperFunc" USING JAR 'hdfs:///udfFunc/hiveUdf.jar';
(3)应用测试:
hive> select * from student;
OK
1 zhangSan
2 liSi
3 wangWu
Time taken: 0.038 seconds, Fetched: 3 row(s)
hive> select strToU(name) from student;
OK
ZHANGSAN
LISI
WANGWU
Time taken: 0.073 seconds, Fetched: 3 row(s)
hive> select strToL(name) from student;
OK
zhangsan
lisi
wangwu
Time taken: 0.041 seconds, Fetched: 3 row(s)