Hive行列转换-lateral view的使用

lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据;在此基础上可以对拆分的数据进行聚合。lateral view首先为原始表的每行调用UDTF,UDTF会把一行拆分成一行或者多行,lateral view在把结果组合,产生一个支持别名表的虚拟表。
1、Lateral View语法
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)* fromClause: FROM baseTable (lateralView)*
2、Lateral View功能
Lateral View用于UDTF(user-defined table generating functions)中将行转成列,例如explode().
3、示例
select a1.time,a1.room_count,roomid
from log_room_online_counter_v2 a1 lateral view explode(split(a1.roomids,',')) t as roomid
where a1.pt_day='2017-05-16'
;

with tab_roomid_hour as(
select roomid from (
select roomid
from log_room_online_counter_v2 a1 lateral view explode(split(a1.roomids,',')) tab_rommid as roomid
where a1.pt_day='2017-05-16' and substr(a1.time,1,13)='2017-05-16 16') x
group by roomid
having count(*)>=5),
tab_room as(select id roomid,is_profession from oss_room_v2 where pt_day='2017-05-17')
select a2.is_profession,count(*) online_room_cnt
from tab_roomid_hour a1
inner join tab_room a2 on a1.roomid=a2.roomid
group by a2.is_profession
;

with tab_roomid_hour as(
select roomid,substr(a1.time,1,13) pt_hour
from log_room_online_counter_v2 a1 lateral view explode(split(a1.roomids,',')) tab_rommid as roomid
where a1.pt_day = '2017-05-17'
group by roomid,substr(a1.time,1,13)
having count(*)>=5),
tab_room as(select id roomid,is_profession from oss_room_v2 where pt_day='2017-05-17')
select a1.pt_hour,a2.is_profession,count(*) online_room_cnt
from tab_roomid_hour a1
inner join tab_room a2 on a1.roomid=a2.roomid
group by a1.pt_hour,a2.is_profession
;


你可能感兴趣的:(#,Hive,Sql)