我有一个配置单元表-表A如下:
id | partner | recent_use | count |
1 | ab | 20160101 | 5 |
1 | cd | 20160304 | 12 |
2 | ab | 20160205 | 1 |
2 | cd | 20150101 | 2 |
3 | ab | 20150401 | 4 |
在表A中,我想得到一个像这样的表-表B:
id | partner |
1 | [ ab : { recent_use:20160101, count:5 } , cd : { recent_use:20160304, count:12 } ]
2 | [ ab : { recent_use:20160205, count:1 } , cd : { recent_use:20150101, count:2 } ]
3 | [ ab : { recent_use:20150401, count:4 } ]
基本上,表B是表A的嵌套版本,因此对于给定的ID,来自其每个伙伴的所有数据都被分组到一列中。
我有两个问题:
非常感谢您提供任何帮助。谢谢!
最佳答案
使用UDAF即可轻松实现这一点-用户定义的聚合功能。您可以编写自定义函数使事情变得简单。您可以使用内置函数来做一些事情。试试看。
select id, CONCAT("[", concat_ws(',', collect_set(CONCAT('"', partner, '":{ "recent_use":', recent_use, ', "count":', count, "}"))), "]") as collJ from tableA group by id
在上面的SQL中,您将在字符串中找到ID和collJ,然后可以使用get_json_object函数将其转换为JSON对象。
引用
https://www.qubole.com/resources/cheatsheet/hive-function-cheat-sheet/
https://cwiki.apache.org/confluence/display/Hive/GenericUDAFCaseStudy
关于json - Hive-从另一个非嵌套的Hive表创建一个嵌套的Hive表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881943/