Hive自定义UDF的JAR包加入运行环境的方法


Hive开发udf函数打包jar文件后,需将jar文件放入hive的运行环境,方法有三。

hive(default) > add jar /opt/module/hive/lib/udf-1.0-SNAPSHOT.jar;

1、进入hive,添加jar,
add jar /opt/module/hive/lib/hiveudf.jar
2、创建一个临时函数
// create temporary function my_lower as 'com.example.hive.udf.LowerCase';

create temporary function myToLow as 'My_UDF';
3、调用
select myToLow(name) from teacher;


先将http://blog.csdn.net/fjssharpsword/article/details/70265554中重定义的两个类打包成DefTextInputFormat.jar,并放到/home/hdfs目录下。


1、方法一:使用add jar命令


1)在hive命令行下执行:add jar /home/hdfs/DefTextInputFormat.jar;

Added [/home/hdfs/DefTextInputFormat.jar] to class path
Added resources: [/home/hdfs/DefTextInputFormat.jar]
该方法每次启动Hive的时候都要从新加入,退出hive就会失效。


2)验证,建表语句如下:

ROW FORMAT DELIMITED
FIELDS TERMINATED BY '#'
STORED AS INPUTFORMAT
'com.hive.DefTextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
'hdfs://nameservice-ha/pgw/gz';


查询后文件和表字段一致。

2、hive-site.xml文件配置hive.aux.jars.path:


配置参考如下:

hive.aux.jars.path
/opt/module/hive/lib/test.jar,file:///jarpath/test.jar

create function myToLow as 'My_UDF';

drop function myToLow;
该方法不需要每次启动Hive执行命令加入,需要配置文件。


3、根目录${HIVE_HOME}下创建文件夹auxlib,然后将自定义jar文件放入该文件夹中;


该方法方便快捷,不过对于客户端操作环境就不能执行。
————————————————
版权声明:本文为CSDN博主「fjssharpsword」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fjssharpsword/article/details/70271671

 1   
 2         
 3             org.apache.logging.log4j
 4             log4j-core
 5             2.8.2
 6         
 7         
 8         
 9             org.apache.hadoop
10             hadoop-common
11             3.1.3
12         
13         
14         
15             org.apache.hadoop
16             hadoop-client
17             3.1.3
18         
19         
20         
21             org.apache.hadoop
22             hadoop-hdfs
23             3.1.3
24         
25 
26         
27             org.apache.hive
28             hive-exec
29             1.2.1
30         
31 
32         
33             org.apache.hive
34             hive-jdbc
35             1.2.1
36         
37 
38         
39             junit
40             junit
41             4.13.2
42             test
43         
44     
1 public class My_UDF extends UDF {
2     public String evaluate(String str){
3         return str.toLowerCase();
4     }
5 }