通过 shell 脚本执行 hive udf

hive udf execution via shell script

我有一个在 hive 终端中运行良好的 Hive Udf,我想通过 shell 脚本执行它。 在配置单元终端上,我能够执行以下命令:

use mashery_db;
add jar hdfs://nameservice1/tmp/nextdata_aggregations/custom_jar/readerCheck.jar;
add file hdfs://nameservice1/tmp/GeoLite2-City.mmdb;
CREATE TEMPORARY FUNCTION geoip AS 'com.mashery.nextdata.hive.udf.GeoIPGenericUDF';

但是当我在 shell 脚本中添加上面的代码时

hive -e "use mashery_db;"
hive -e "add jar hdfs://nameservice1/tmp/nextdata_aggregations/custom_jar/readerCheck.jar;"
hive -e "add file hdfs://nameservice1/tmp/GeoLite2-City.mmdb;"
hive -e "CREATE TEMPORARY FUNCTION geoip AS 'com.mashery.nextdata.hive.udf.GeoIPGenericUDF';"

第一个 'hive -e' 运行良好并添加了 jar,但最后一个创建临时函数不起作用。我遇到以下错误:

FAILED: ParseException line 1:35 mismatched input 'com' expecting StringLiteral near 'AS' in create function statement

我也试过用单引号

hive -e "CREATE TEMPORARY FUNCTION geoip AS 'com.mashery.nextdata.hive.udf.GeoIPGenericUDF';"

然后我得到 FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.FunctionTask

FAILED: Class com.mashery.nextdata.hive.udf.GeoIPGenericUDF not found

hive Udf 是否支持 shell 脚本,如果它做错了我在做什么。提前致谢

每次调用 hive -e 都会生成一个新进程,其中包含一个新配置单元 shell,它不记得前一个配置单元所做的事情,因此配置单元 'forgets' UDF 所在的位置.. . 一种解决方案是将它们链接在一个命令中,但更好的形式是将所有配置单元命令放在一个文件中(例如 "commands.hql")并使用 hive -f commands.hql 代替 -e.

文件看起来像这样:

use mashery_db;
add jar hdfs://nameservice1/tmp/nextdata_aggregations/custom_jar/readerCheck.jar;
add file hdfs://nameservice1/tmp/GeoLite2-City.mmdb;
CREATE TEMPORARY FUNCTION geoip AS 'com.mashery.nextdata.hive.udf.GeoIPGenericUDF';"

您可以使它与 hive -ehive -f 一起使用:

hive -e "use mashery_db;
add jar hdfs://nameservice1/tmp/nextdata_aggregations/custom_jar/readerCheck.jar;
add file hdfs://nameservice1/tmp/GeoLite2-City.mmdb;
CREATE TEMPORARY FUNCTION geoip AS 'com.mashery.nextdata.hive.udf.GeoIPGenericUDF';"

将它们创建为文件并使用 hive -f hive_file.hql 也可以。