简体   繁体   English

Hive UDF传递数组 <string> 作为参数

[英]Hive UDF pass array<string> as parameter

I'm trying to pass in an array to a Hive UDF via collect_set : 我正在尝试通过collect_set将数组传递给Hive UDF

SELECT ..., collect_set(...) FROM ...;

And my Hive UDF wants to take in this array and append the first letter of each array element to an output string: 我的Hive UDF想要接受此数组,并将每个数组元素的第一个字母附加到输出字符串:

public class MyUDF extends UDF {

public String evaluate(String[] array) {    
    String output = "";

    // Check for valid argument
    if (array == null) return output;

    try {
        // Add first character of every array element to output string
        for (int i = 0; i < array.length; i++) {
            output += array[i].charAt(0);

            // If there is another array element after this one, append DELIMITER
            if (i + 1 < array.length) output += ",";
        }
    } catch (Exception e) { 
        System.out.println(e.getMessage());
        System.exit(1);
    }
    return output;
}

But the issue I get when I try to run: 但是当我尝试运行时遇到的问题是:

ADD JAR ./list_builder.jar;
CREATE TEMPORARY FUNCTION build_list as 'MyCustomUDF.MyUDF';

SELECT ..., build_list(collect_set(description)) FROM ...;

...
FAILED: SemanticException [Error 10014]: Line 142:21 Wrong arguments 'description': No matching method for class MyCustomUDF.MyUDF with (array<string>). Possible choices: _FUNC_(struct<>)

I've tried changing String[] to ArrayList and List but I'm still hitting the same error. 我尝试将String[]更改为ArrayListList但仍然遇到相同的错误。

Note : The output of collect_set is something like: [L-ADD", "P-OAN", "P-OAH"] , so I'm expecting an output from my UDF like: L,P,P . 注意collect_set的输出类似于: [L-ADD", "P-OAN", "P-OAH"] ,因此我期望UDF的输出诸如: L,P,P

Any ideas? 有任何想法吗?

Thanks. 谢谢。

Following @kostya's answer, I used substr : 遵循@kostya的回答,我使用了substr

SELECT ..., collect_set(substr(description,0,1)) FROM ...;

Which meant I didn't need a UDF. 这意味着我不需要UDF。

Thanks. 谢谢。

Try ArrayList<String> instead of String[] because hive sends array as array<String> not String[] 尝试使用ArrayList<String>而不是String[]因为配置单元将数组作为array<String>而不是String[]

public class MyUDF extends UDF {

public String evaluate(ArrayList<String> array) {    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM