如何将配置从hive脚本传递到UDF

问题描述 投票:3回答:4

在猪中,您可以通过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的值?

java hadoop hive apache-pig
4个回答
2
投票

您可以尝试继承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();  
}

0
投票

转到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;

0
投票

从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);
      }

-3
投票

有很多例子,共享,所以你可以在谷歌上找到所有必需的细节:)。

在共享链接中描述的小示例:

hive> ADD JAR assembled.jar;
hive> create temporary function hello as 'com.test.example.UDFExample';
hive> select hello(firstname) from people limit 10;

请查看链接以供我通常使用的参考:Link1 Link2

© www.soinside.com 2019 - 2024. All rights reserved.