在猪中,您可以通过UDFContext
将猪脚本中的配置传递给猪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'
,我如何在java UDF中检索它,需要消耗MY_CONF
的值?
您可以尝试继承UDF
,而不是扩展GenericUDF
类。此类具有以下可以覆盖的方法:
/**
* 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
一样的方法来检索Job配置。所以你可以做以下事情:
@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);
}