hive行转多列LATERAL VIEW explode

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》	灾难

猜你喜欢

转载自blog.csdn.net/weixin_38842096/article/details/84640251