[英]How to pass configurations from hive script to UDF
在pig中,您可以通过UDFContext
将配置从pig脚本传递到pig UDF。 例如,
// in pig script
SET my.conf dummy-conf
// in UDF java code
Configuration conf = UDFContext.getUDFContext().getJobConf();
String myConf = conf.get("my.conf");
那么,是否有类似的方法将配置从配置单元脚本传递到配置单元UDF? 例如,如果我在hive脚本中set MY_CONF='foobar'
,我如何在需要消耗MY_CONF
值的java UDF中检索它?
您可以尝试GenericUDF
,而不是扩展UDF
类。 此类具有以下可以覆盖的方法:
/**
* Additionally setup GenericUDF with MapredContext before initializing.
* This is only called in runtime of MapRedTask.
*
* @param context context
*/
public void configure(MapredContext context) {
}
MapredContext
有一个方法,就像Pig的UDFContext
一样,可以检索作业配置。 所以你可以做以下事情:
@Override
public void configure(MapredContext context) {
Configuration conf = context.getJobConf();
}
转到hive命令行
hive> set MY_CONF='foobar';
在命中命令时应列出您的变量
hive> set;
现在,考虑一下你
Jar :MyUDF.jar
UDF calss :MySampleUDF.java接受String值。
表 :员工
hive> ADD JAR /MyUDF.jar
hive> CREATE TEMPORARY FUNCTION testUDF AS 'youpackage.MySampleUDF';
hive> SELECT testUDF(${MY_CONF}) from employee;
从hive 1.2开始,有两种方法。
1.从GenericUDF覆盖配置方法
@Override
public void configure(MapredContext context) {
super.configure(context);
someProp = context.getJobConf().get(HIVE_PROPERTY_NAME);
}
以上(1)不适用于所有情况。 仅适用于MapredContext。 每个查询都必须是强制映射/减少作业,才能执行该操作
set hive.fetch.task.conversion=minimal/none;
set hive.optimize.constant.propagation=false;
。 如果设置了以上属性,您将遇到主要的性能问题,尤其是对于较小的查询。
2.使用SessionState
SessionState ss = SessionState.get();
if (ss != null) {
this.hiveConf = ss.getConf();
someProp = this.hiveConf.get(HIVE_PROPERTY_NAME);
LOG.info("Got someProp: " + someProp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.