5.7 列转行查询
需求
有以下数据:

想要的结果:

步骤1: 准备数据, 创建本地文件:movie.txt
《疑犯追踪》 悬疑,动作,科幻,剧情
《Lie to me》 悬疑,警匪,动作,心理,剧情
《战狼2》 战争,动作,灾难
步骤2: 创建表, 并上传数据
create table movie_info(
movie string,
category array<string>
)
row format delimited
fields terminated by "\t"
collection items terminated by ",";
load data local inpath "/opt/module/datas/movie.txt" into table movie_info;
步骤3: 按需求查询函数
select
movie,
category_name
from
movie_info lateral view explode(category) table_tmp as category_name;

说明:
EXPLODE(col):将hive一列中复杂的array或者map结构拆分成多行。 我们的案例中把category这个Array拆分成多行.LATERAL VIEW用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias解释:用于和split, explode等UDTF一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。