How to concatenate the elemets of int array to string in Hive

自古美人都是妖i 提交于 2019-12-18 07:14:44

问题


I'm trying to concatenate the element of int array to one string in hive.

The function concat_ws works only for string arrays, so I tried cast(my_int_array as string) but it's not working.

Any suggestion?


回答1:


Try to transform using /bin/cat:

from mytable select transform(my_int_array) using '/bin/cat' as (my_int_array);

Second option is to alter table and replace delimiters:

1) ALTER TABLE mytable  CHANGE COLUMN my_int_array = my_int_array_string string;
2) SELECT REPLACE(my_int_array_string, '\002', ', ') FROM mytable;



回答2:


It seems that the easiest way is to write a custom UDF to perform this specific task:

public class ConcatIntArray extends UDF {
   public String evaluate(ArrayList<Integer> in, final String delimiter){
       return in.stream().map(u-> String.valueOf(u)).collect(Collectors.joining(delimiter));
   }
} 


来源:https://stackoverflow.com/questions/40977674/how-to-concatenate-the-elemets-of-int-array-to-string-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!