hive行轉多列LATERAL VIEW explode
阿新 • • 發佈:2018-12-01
lateral view用於和split、explode等UDTF一起使用的,能將一行資料拆分成多行資料,在此基礎上可以對拆分的資料進行聚合,lateral view首先為原始表的每行呼叫UDTF,UDTF會把一行拆分成一行或者多行,lateral view在把結果組合,產生一個支援別名表的虛擬表.
資料:以製表符分隔
movie category
《疑犯追蹤》 懸疑,動作,科幻,劇情
《Lie to me》 懸疑,警匪,動作,心理,劇情
《戰狼 2》 戰爭,動作,災難
在hive中建立表及匯入資料
create table movie_info( movie string, category array<string>) row format delimited fields terminated by "\t" collection items terminated by ","; //匯入資料 load data local inpath "movie_info.tsv" into table movie_info;
將電影分類中的陣列資料展開
select movie, category_name
from movie_info lateral view explode(category) table_tmp as category_name;
結果如下:
《疑犯追蹤》 懸疑
《疑犯追蹤》 動作
《疑犯追蹤》 科幻
《疑犯追蹤》 劇情
《Lie to me》 懸疑
《Lie to me》 警匪
《Lie to me》 動作
《Lie to me》 心理
《Lie to me》 劇情
《戰狼 2》 戰爭
《戰狼 2》 動作
《戰狼 2》 災難