Hive之行转列/列转行

1、行转列

场景:在hive表中,一个用户会有多个人群标签,List格式(逗号分隔如要转成List),有时我们需要统计一个人群标签下有少用户,这是就需要使用行转列了

例如,user_crowd_info有如下数据

visit_id    crowds
abc         [100,101,102]
def         [100,101]
abe         [101,105]

可以使用的函数

select explode(crowds) as crowd from user_crowd_info;

结果:
100
101
102
100
101
101
105

这样执行的结果只有crowd, 但是我们需要完整的信息,使用select visit_id, explode(crowds) as crowd from user_crowd_info;是不对的,会报错UDTF's are not supported outside the SELECT clause, nor nested in expressions

所以我们需要这样做:

select visit_id,crowd 
from user_crowd_info t lateral view explode(t.crowds) adtable as crowd;

2、列转行

使用concat_ws函数即可

select visit_id,concat_ws(,crowd) from user_crowd_info group by visit_id;

《Hive之行转列/列转行》 欢迎订阅「K叔区块链」 – 专注于区块链技术学习

博客地址:
http://www.jouypub.com

简书主页:
https://www.jianshu.com/u/756c9c8ae984

segmentfault主页:
https://segmentfault.com/blog/jouypub

腾讯云主页:
https://cloud.tencent.com/developer/column/72548

    原文作者:JouyPub
    原文地址: https://www.jianshu.com/p/88f5ad4ea11f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞