Grouping hive rows in an array of this rows

徘徊边缘 提交于 2019-12-03 12:49:21

问题


I have a table like the following :

User:String Alias:String
JohnDoe     John
JohnDoe     JDoe
Roger       Roger

And I would like to group all the aliases of an user in an array, in a new table which would look like this :

User:String Alias:array<String>
JohnDoe     [John, JDoe]
Roger       [Roger]

I can't figure out how to do that with HiveQL.Do I have to write an UDF for that ?

Thanks !


回答1:


Check out the built-in aggregate function collect_set.

select 
    User, 
    collect_set(Alias) as Alias
from table
group by User;


来源:https://stackoverflow.com/questions/16836702/grouping-hive-rows-in-an-array-of-this-rows

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