In Hive, how to combine multiple tables to produce single row containing array of objects?

让人想犯罪 __ 提交于 2020-02-03 01:56:06

问题


I have two tables as follows:

users table
==========================  
| user_id   name     age |  
|=========================  
|  1        pete      20 |  
|  2        sam       21 |  
|  3        nash      22 |  
==========================

hobbies table
======================================
| user_id   hobby         time_spent |
|=====================================
|  1        football          2      |
|  1        running           1      |
|  1        basketball        3      |
======================================

First question: I would like to make a single Hive query that can return rows in this format:

{ "user_id":1, "name":"pete", "hobbies":[ {hobby: "football", "time_spent": 2}, {"hobby": "running", "time_spent": 1}, {"hobby": "basketball", "time_spent": 3} ] }

Second question: If the hobbies table were to be as follows:

========================================
| user_id   hobby             scores   |
|=======================================
|  1        football          2,3,1    |
|  1        running           1,1,2,5  |
|  1        basketball        3,6,7    |
========================================

Would it be possible to get the row output where scores is a list in the output as shown below:

{ "user_id":1, "name":"pete", "hobbies":[ {hobby: "football", "scores": [2, 3, 1]}, {"hobby": "running", "scores": [1, 1, 2, 5]}, {"hobby": "basketball", "scores": [3, 6, 7]} ] }

回答1:


I was able to find the answer to my first question

select u.user_id, u.name, 
       collect_list(
           str_to_map(
                concat_ws(",", array(
                    concat("hobby:", h.hobby), 
                    concat("time_spent:", h.time_spent)
                ))
           )
       ) as hobbies
from users as u
join hobbies as h on u.user_id=h.user_id
group by u.user_id, u.name;


来源:https://stackoverflow.com/questions/56088344/in-hive-how-to-combine-multiple-tables-to-produce-single-row-containing-array-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!