所以,我有一张表
user_id, product, price
12, foobar, 1.2
12, foo, 2.2
13, baz, 2
14, biz, 20
14, buzz, 21
and so on..
我想按用户ID分组,基本上存储为以下架构
schema:
user_id int,
product_prices string
,结果是
12\t foobar,1.2\tfoo,2.2
13\t baz,2
14\t biz,20\tbuzz,21
等等
我如何在hive(sqlish)中执行此操作?
答案 0 :(得分:1)
可以使用CTE,CONCAT和concat_ws
完成尝试以下代码
with temp as
( select user_id, CONCAT(product , ',' , cast(price as String)) res from your_table)
SELECT user_id, concat_ws("\t",collect_set(res))
FROM temp
GROUP BY user_id;
步骤1 CONCAT产品和价格与","
步骤2将concat_ws用于" \ t"到CONCAT数组。
答案 1 :(得分:1)
select user_id
,concat_ws('\t',collect_list(concat_ws(',',product,cast(price as string))))
from mytable
group by user_id
;
+----+----------------------+
| 12 | foobar,1.2 foo,2.2 |
| 13 | baz,2 |
| 14 | biz,20 buzz,21 |
+----+----------------------+
...如果字典(地图)真的是你想要的:
select user_id
,str_to_map(concat_ws(',',collect_list(concat_ws(':',product,cast(price as string)))))
from mytable
group by user_id
;
+----+------------------------------+
| 12 | {"foobar":"1.2","foo":"2.2"} |
| 13 | {"baz":"2"} |
| 14 | {"biz":"20","buzz":"21"} |
+----+------------------------------+