简体   繁体   English

如何拆分逗号分隔的字符串并在配置单元中收集唯一值?

[英]how to split comma separated string and collect unique values in hive?

I have one hive table with two column.我有一个带有两列的蜂巢表。 Type of both column is string.两列的类型都是字符串。 One is simple client id and other is command separated string of item ids.一个是简单的客户端 ID,另一个是命令分隔的项目 ID 字符串。 There can be multiple row with same client id but different item id string.可以有多行具有相同的客户端 ID 但不同的项目 ID 字符串。

I want one hive query which generate table with two column.我想要一个生成两列表的配置单元查询。 one is client id and other is comma separated string with all unique item ids.一个是客户端 ID,另一个是逗号分隔的字符串,其中包含所有唯一的项目 ID。

Data in original table:原始表中的数据:

Client Id       Item Ids
1               1,2,3,4
2               3,4,6,8
4               4,5,1,3
2               3,4,7,8
3               5,6,8,2
4               7,8,9,4

Query should generate this result查询应生成此结果

 Client Id       Item Ids
 1               1,2,3,4
 2               3,4,7,6,8
 4               4,5,1,3,7,8,9
 3               5,6,8,2

Use explode() and collect_set() to get unique set, aggregate string using concat_ws and group by Client_id :使用explode()collect_set()来获得独特的一套,使用聚合串concat_ws和group by Client_id

hive> select Client_id, concat_ws(',',collect_set(item_id)) as item_ids
    > from test_t lateral view explode(split(item_ids,',')) a as item_id
    > group by Client_id;

Output:输出:

OK
1       1,2,3,4
2       3,4,6,8,7
3       5,6,8,2
4       4,5,1,3,7,8,9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM