我有两个表,如下所示:
users table
==========================
| user_id name age |
|=========================
| 1 pete 20 |
| 2 sam 21 |
| 3 nash 22 |
==========================
hobbies table
=========================
| user_id hobby |
|========================
| 1 football |
| 1 running |
| 1 basketball |
=========================
我想做一个单一的Hive查询,该查询可以返回以下格式的行:
{ "user_id":1, "name":"pete", "hobbies":["football", "running", "basketball"] }
答案 0 :(得分:1)
加入表并使用collect_list
将每个用户的兴趣爱好放入array
中。
select u.user_id,u.name,collect_list(h.hobby) as hobbies
from users u
join hobbies h on u.user_id=h.user_id
group by u.user_id,u.name
使用collect_set
获取唯一值,以防重复。