[英]Concat multiple rows with a delimiter in Hive
I need to concat string values row wise with '~' as delimiter.我需要使用 '~' 作为分隔符逐行连接字符串值。 I have the following data:
我有以下数据:
I need to concat 'Comment' column for each 'id' in the ascending order of 'row_id' with '~' as delimiter.我需要按“row_id”的升序连接每个“id”的“评论”列,并使用“~”作为分隔符。
Expected output is as below:预期输出如下:
GROUP_CONCAT
is not an option since its not recognized in my Hive version. GROUP_CONCAT
不是一个选项,因为它在我的 Hive 版本中无法识别。 I can use collect_set
or collect_list
, but I won't be able to insert delimiter in between.我可以使用
collect_set
或collect_list
,但我无法在两者之间插入分隔符。
Is there any workaround?有什么解决方法吗?
collect_list returns array, not string. collect_list返回数组,而不是字符串。
Array can be converted to delimited string using concat_ws .可以使用concat_ws将数组转换为分隔字符串。
This will work, with no specific order of comments.这将起作用,没有特定的评论顺序。
select id
,concat_ws('~',collect_list(comment)) as comments
from mytable
group by id
;
+----+-------------+
| id | comments |
+----+-------------+
| 1 | ABC~PRQ~XYZ |
| 2 | LMN~OPQ |
+----+-------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.