string 如何在 hive sql 中将数组转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38711201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert array to string in hive sql?
提问by Bethlee
I want to convert an array to string in hive. I want to collect_set array values to convert to string without [[""]]
.
我想在 hive 中将数组转换为字符串。我想将 collect_set 数组值转换为没有[[""]]
.
select actor, collect_set(date) as grpdate from actor_table group by actor;
so that [["2016-07-01", "2016-07-02"]]
would become 2016-07-01, 2016-07-02
所以这[["2016-07-01", "2016-07-02"]]
将成为2016-07-01, 2016-07-02
回答by leftjoin
Use concat_ws(string delimiter, array<string>)
function to concatenate array:
使用concat_ws(string delimiter, array<string>)
函数连接数组:
select actor, concat_ws(',',collect_set(date)) as grpdate from actor_table group by actor;
If the date field is not string, then convert it to string:
如果日期字段不是字符串,则将其转换为字符串:
concat_ws(',',collect_set(cast(date as string)))
Read also this answer about alternative ways if you already have an array (of int) and do not want to explode it to convert element type to string: How to concatenate the elements of int array to string in Hive
如果您已经有一个(int)数组并且不想分解它以将元素类型转换为字符串,请阅读有关替代方法的答案:How to concatenate the elements of int array to string in Hive