如何在 hive sql 中将数组转换为字符串? [英] How can I convert array to string in hive sql?
问题描述
我想在 hive 中将数组转换为字符串.我想在没有 [[""]]
的情况下将 collect_set 数组值转换为字符串.
I want to convert an array to string in hive. I want to collect_set array values to convert to string without [[""]]
.
select actor, collect_set(date) as grpdate from actor_table group by actor;
这样 [["2016-07-01", "2016-07-02"]]
会变成 2016-07-01, 2016-07-02
推荐答案
使用 concat_ws(string delimiter, array
函数连接数组:
Use concat_ws(string delimiter, array<string>)
function to concatenate array:
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)))
如果您已经有一个(int)数组并且不想分解它以将元素类型转换为字符串,请阅读有关替代方法的答案:如何在Hive中将int数组的元素连接成字符串
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
这篇关于如何在 hive sql 中将数组转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!