需求:将NBA总冠军球队数据使用explode进行拆分,并且根据夺冠年份进行倒序排序。
- --step1:建表
- create table the_nba_championship(
- team_name string,
- champion_year array<string>
- ) row format delimited
- fields terminated by ','
- collection items terminated by '|';
load data inpath '/input/The_NBA_Championship.txt' into table the_nba_championship;

- p3:验证数据
- select * from the_nba_championship;
可以使用order by,但效率太低
- --只查询冠军年份,降序排序
- select explode(champion_year) as champ
- from the_nba_championship order by champ ; --order by 太占用资源,效率低
使用炸裂函数,配合侧视图,制作新表
- -- 将NBA总冠军球队数据使用explode进行拆分,并且根据夺冠年份进行倒序排序。
- --先将冠军年各个炸出来
- select explode(champion_year) from the_nba_championship;
-
- --再将炸出来的结果用侧视图保存,和原表的球队名字拼起来
-
- with nba as
- (select team_name, c.champ_year_explode
- from the_nba_championship
- lateral view explode(champion_year) c as champ_year_explode
- --冠军年份炸开后,存到虚拟表c,并起了个字段名,与原表进行拼接.
- --并将拼接完成的表起了个别名,作为排序查询的准备
- )
- select * from nba order by champ_year_explode desc ;
结果
