Eclipse编写自定义UDF函数步骤
1、配置 pom
添加两个dependency
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.2</version>
</dependency>

2、下载 hive-site.xml,放到 bin目录下,刷新maven项目
工程目录 右键 -> Maven -> Update Project

3、编写功能代码

4、打成Jar包,并上传到服务器(略)


5、在Hive中关联Jar,并创建临时函数 (3种方法)
5.1 本地临时函数
add jar local_path;
hive (db02)> add jar /opt/datas/udf_time.jar;

create temporary function 函数名 as ‘包名.类名';
hive (db02)> create temporary function mytime as 'com.bigdata.hive.HiveUdfTime';

5.2 HDFS临时函数
或者可以直接上传到HDFS上,
hive (db02)> dfs -put /opt/datas/udf_time.jar /;

create temporary function 函数名 as '包名.类名' using jar 'hdfs_path' ;
hive (db02)> create temporary function mytime1 as 'com.bigdata.hive.HiveUdfTime' using jar 'hdfs://hadoop06:8020/udf_time.jar';

5.3 永久函数
将Hive的jar添加到Hive的环境变量,并且编译Hive的源码
6、查看自定义函数
hive (db02)> show functions;
7、使用自定义函数
hive (db02)> select time_local,mytime1(time_local) newtimes from apachelog3;
网友评论