如何将多个列聚合为HIVE中每个组的JSON文件

时间:2017-06-05 06:01:18

标签: sql json hive

我想将下表转换为包含两行的新表。在新表中,每个user_id都有一个JSON文件来汇总第一个表中的所有其他列。

user_id reservation_id nights price AAA 10001 1 100 AAA 10002 1 120 BBB 20003 7 350 user_id reservation_details AAA {10001:{'nights':1, 'price':100}, 10002:{'nights':1, 'price':120}} BBB {20003:{'nights':7, 'price':350}}

1 个答案:

答案 0 :(得分:1)

select      user_id
           ,concat("{",concat_ws(',',collect_list(concat(string(reservation_id),":{'nights':",string(nights),",'price':",string(price),"}"))),"}")  as reservation_details

from        mytable

group by    user_id       
+---------+-----------------------------------------------------------------+
| user_id |                       reservation_details                       |
+---------+-----------------------------------------------------------------+
| AAA     | {10001:{'nights':1,'price':100},10002:{'nights':1,'price':120}} |
| BBB     | {20003:{'nights':7,'price':350}}                                |
+---------+-----------------------------------------------------------------+