我使用以下代码来连接结果
选择COLLECT_LIST(col_name)AS my_col来自my_table这有点达到了我想要的结果,输出如下:
[ “车”, “摩托车”, “公共汽车”][…
hive> with t1 as (select cast(1 as string) as col1 union select cast(2 as string) as col1 union select cast(3 as string) as col1) select collect_set(col1), concat_ws(',', collect_set(col1)) from t1; OK ["1","2","3"] 1,2,3 Time taken: 95.27 seconds, Fetched: 1 row(s)
concat_ws只接受字符串值数组
如果项目数量是固定的,那么您也可以使用
hive> with t1 as (select cast(1 as string) as col1 union select cast(2 as string) as col1 union select cast(3 as string) as col1) select concat(collect_set(col1)[0], ',' ,collect_set(col1)[1], ',', collect_set(col1)[2]) from t1;
使用 concat_ws 从一开始就加入字符串数组:
concat_ws
SELECT concat_ws(', ', collect_set(col_name)) AS my_col FROM my_table ^ ---------------------------------- ^
该 concat_ws 因为你有一个字符串数组,而不是一个字符串,所以更合适。