Hive中如何添加自定义UDF函数以及oozie中使用hive的自定义函数

操作步骤:

1. 修改.hiverc文件

在hive的conf文件夹下面,如果没有.hiverc文件,手工自己创建一个。

参照如下格式添加:

add jar /usr/local/hive/external_lib/gw-hive-udf.jar;
create temporary function myexp as 'com.gw.hive.udf.udtf.ExplodeEx';

第一行为自定义jar包存放的路径。

第二行为定义的funtion全类名,并且指定函数名为myexp;

2.上传jar包

将jar包上次到上面的目录中/usr/local/hive/external_lib

重启hive就可以使用。

3.oozie中安装

如果在oozie中调度hive的脚本时,需要用到该自定义包,则需要单独配置。

oozie调用时,使用到的依赖都包是保存在hdfs上的。

如果是hive通用的jar包,则放到hdfs上的oozie指定的hive目录中。

/user/oozie/share/lib/hive

如果属于某个程序单独用的依赖jar包,则上传到程序所在目录的lib文件夹中。

比如:/user/oozie/app/guba_loginlog/lib/,比如

然后需要使用以下命令刷新oozie的共享包。

oozie admin -sharelibupdate

在oozie调用的脚本中同样需要添加:

create temporary function myexp as 'com.gw.hive.udf.udtf.ExplodeEx';

190806更新:

1. 准备条件

上传文件到hdfs上:

# 上传文件到hdfs
hdfs dfs -put encryptAll-1.0.jar /user/hive/udf/

2.1 hive中添加临时自定义函数

#临时
add jar hdfs://ns1/user/hive/udf/udf/encryptAll-1.0.jar;
或者
add jar /opt/local/hive/udf/encryptAll-1.0.jar;


# 创建函数:
create temporary function encrypt_all as 'com.test.udf.EncryptAll';
create temporary function decrypt_all as 'com.test.udf.DecryptAll';

2.2 hive中添加永久自定义函数

#永久:
create function encrypt_all as 'com.test.udf.EncryptAll' using jar 'hdfs://ns1/user/hive/udf/encryptAll-1.0.jar';
create function decrypt_all as 'com.test.udf.DecryptAll' using jar 'hdfs://ns1/user/hive/udf/encryptAll-1.0.jar';
原文地址:https://www.cnblogs.com/30go/p/8556143.html