我试图通过Hive UDF通过collect_set将一个数组传递给一个
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[]更改为ArrayList和List,但仍然遇到了相同的错误。
注意事项:collect_set的输出类似于:[L-ADD", "P-OAN", "P-OAH"],所以我希望从UDF:L,P,P得到一个输出。
有什么想法吗?
谢谢。
发布于 2015-05-27 02:40:16
在@kostya的回答之后,我使用了substr
SELECT ..., collect_set(substr(description,0,1)) FROM ...;这意味着我不需要UDF。
谢谢。
发布于 2016-06-30 12:14:17
尝试ArrayList<String>而不是String[],因为hive以array<String>而不是String[]的形式发送数组
public class MyUDF extends UDF {
public String evaluate(ArrayList<String> array) {
}https://stackoverflow.com/questions/30433047
复制相似问题