Hive UDF函数开发

Hive学习笔记系列,Hive UDF函数开发

1.在eclipse中新建java project

然后新建class package(com.jd.lhb) name(UDFLower)

2.添加jar library

添加hadoop-core-1.0.3.jar hive-exec-0.8.1.jar两个文件到project
build path

3.编写代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.jd.lhb;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

@Description(name="my_lower",
value="_FUNC_(String) - transfer the upper string to lowwer",
extended="Example:\n"
+ " > select _FUNC_(String) from src;\n")
public class UDFLower extends UDF{

/**
* @author lhb
* @param s
* 将大写的字符串s transfer to小写的字符串
*/
public Text evaluate(final Text s){
if(null==s){
return null;
}
return new Text(s.toString().toLowerCase());
}
}

4.编译打包文件为HiveUDF.jar

5.加载至hive中

1
2
3
hive> add jar /home/hadoop/udf/HiveUDF.jar
Added udf_hive.jar to class path
Added resource: udf_hive.jar

6.测试准备

6.1data.txt文件内容为

1
2
3
4
WHO
AM
I
HELLO

6.2创建测试数据表

hive> create table dual (info string);

6.3导入数据文件data.txt

1
hive> load data local inpath '/home/hadoop/data.txt' into table dual;

6.4创建udf函数

1
hive> create temporary function my_lower as 'com.afan.UDFLower';

7.测试

1
2
3
hive> select info from dual;
使用udf函数
hive> select my_lower(info) from dual;

关于作者

转载请注明出处:
https://crazylook.github.io/2014/06/10/hive/notes-chapter13-udf/