【问题标题】:Hive UDF ExecutionHive UDF 执行
【发布时间】:2012-10-12 20:27:16
【问题描述】:

我编写了一个使用内部 API 进行解密的 Hive UDF,如下所示:

public Text evaluate(String customer) {
    String result = new String();

    if (customer == null) { return null; }

    try {
        result = com.voltage.data.access.Data.decrypt(customer.toString(), "name");
    } catch (Exception e) {
        return new Text(e.getMessage());
    }

    return new Text(result);
}

Data.decrypt 确实如此:

public static String decrypt(String data, String type) throws Exception {
    configure();
    String FORMAT = new String();
    if (type.equals("ccn")) {
        FORMAT = "CC";
    } else if (type.equals("ssn")) {
        FORMAT = "SSN";
    }   else if (type.equals("name")) {
        FORMAT = "AlphaNumeric";
    }

    return library.FPEAccess(identity, LibraryContext.getFPE_FORMAT_CUSTOM(),String.format("formatName=%s", FORMAT),authMethod, authInfo, data);
}

其中 configure() 创建了一个非常昂贵的上下文对象。

我的问题是:Hive 是否为查询返回的每一行执行一次此 UDF?即如果我选择 10,000 行,评估方法是否会运行 10,000 次?

我的直觉告诉我是的。如果是这样,那么这是第二个问题:

有什么办法可以做到以下几点:

a) 在查询开始时运行一次configure(),然后共享上下文对象

b) 它不是 UDF 返回一个解密的字符串,而是将加密的字符串聚合到某个 Set 中,然后我对这个集合进行批量解密?

提前致谢

【问题讨论】:

    标签: hadoop hive


    【解决方案1】:

    configure() 是否需要每个 JVM 调用一次,或者每个 UDF 类的实例调用一次?

    如果每个 JVM 一次,只需将其放在类中的静态块中,如下所示:

    static {
        configure();
    }
    

    如果每个实例一次,则将其放入构造函数中:

    public [class name]() {
        super();
        configure();
    }
    

    【讨论】:

    • 是的。发现我可以在发布问题大约两分钟后执行 static {}
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多