我希望在Hive中将一个数组转换为字符串。我想要收集数组值并将其转换为字符串,而不包含[[""]]
。
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
我希望在Hive中将一个数组转换为字符串。我想要收集数组值并将其转换为字符串,而不包含[[""]]
。
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<string>)
函数来合并数组:
select actor, concat_ws(',',collect_set(date)) as grpdate from actor_table group by actor;
如果日期字段不是字符串,则将其转换为字符串:concat_ws(',',collect_set(cast(date as string)))
如果您已经有一个整数数组并且不想将其拆分以将元素类型转换为字符串,请阅读此答案中关于替代方法的内容:如何在Hive中将int数组的元素连接成字符串
SELECT CAST(COLLECT_SET(date) AS STRING) AS dates FROM actor_table
PS:我需要这个功能,但只找到了有关数组转换为字符串的问题。