【问题标题】:How to pass Hive conf variable in hive udf?如何在 hive udf 中传递 Hive conf 变量?
【发布时间】:2014-07-22 14:46:27
【问题描述】:

我想将 hive conf 变量传递给 hive UDF。

下面是代码sn-p。

hive -f ../hive/testHive.sql -hivevar testArg=${testArg}

下面是 hive UDF 调用。

select setUserDefinedValueForColumn(columnName,'${testArg}') from testTable;

在 udf 中,我将 testArg 的值设为 null。

请告诉我如何在 udf 中使用 hive conf 变量以及如何在 hive UDF 中访问 Hive 配置?

【问题讨论】:

    标签: hadoop hive bigdata


    【解决方案1】:

    我认为您应该使用以下命令将 hive 变量作为“hiveconf”传递:

    hive --hiveconf testArg="my test args" -f ../hive/testHive.sql
    

    那么您可能在GenericUDF evaluate() 方法中包含以下代码:

    @Override
     public Object evaluate(DeferredObject[] args) throws HiveException {
        String myconf;
        SessionState ss = SessionState.get();
        if (ss != null) {
            HiveConf conf = ss.getConf();
            myconf= conf.get("testArg");
            System.out.println("sysout.myconf:"+ myconf);
        }
    }
    

    代码在 hive 1.2 上测试

    【讨论】:

      【解决方案2】:

      您不能通过在视图代码中使用 ${hiveconf:testArg} 将 Hive 变量直接传递给视图,因为在视图创建期间,Hive 将准确获取变量的值,因此视图将是静态的。

      唯一的机会是使用 UDF 访问 hive 变量:

      您可以使用 GenericUDF。它有一个方法 configure ,它以 MapredContext 作为参数。因此,您需要在 GenericUDF 中指定一个配置方法,例如:

      public void configure(MapredContext context){
       yourVar = context.getJobConf().get("hive_variable");
      }
      

      这仅在 MapRedTask 运行时调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 2012-06-16
        • 2016-01-27
        • 2021-11-12
        相关资源
        最近更新 更多