有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java配置单元UDF传递数组<string>作为参数

我试图通过collect_set将数组传递给Hive UDF

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

我的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;
}

但当我尝试跑步时,我遇到的问题是:

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<>)

我尝试过将String[]更改为ArrayListList,但仍然遇到相同的错误

注意collect_set的输出类似:[L-ADD", "P-OAN", "P-OAH"],因此我希望UDF的输出类似:L,P,P

有什么想法吗

谢谢


共 (2) 个答案

  1. # 1 楼答案

    在@kostya的回答之后,我使用了^{

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

    这意味着我不需要UDF

    谢谢

  2. # 2 楼答案

    尝试ArrayList<String>而不是String[],因为hive将数组作为array<String>而不是String[]发送

    public class MyUDF extends UDF {
    
    public String evaluate(ArrayList<String> array) {    
    
    }