关于lateral view 和 explode 很不错的讲解

来源:互联网 发布:企业文化 知乎 编辑:程序博客网 时间:2024/05/08 04:13

转:http://blog.csdn.net/gamer_gyt/article/details/52169441


1:Array

     顾名思义就是数组,使用方式 array<>

     1):创建表

     拿电影数据为例,数据的维度包括


      创建movie_message表:

[html] view plain copy
  1. create table movie_message(  
  2.     id int,  
  3.     title string,  
  4.     daoyan array<string>,  
  5.     bianju array<string>,  
  6.     leixing array<string>,  
  7.     zhuyan array<string>,  
  8.     year int,  
  9.     month int,  
  10.     shichang int,  
  11.     disnum int,  
  12.     score float  
  13. )  
  14. comment "this table about movie's message"  
  15. row format delimited fields terminated by ","  
  16. collection items terminated by '/';  
      加载数据(可以从本地加载,也可以从hdfs装载,当然也可以从别的表中查询结果进行转载),这里从本地装载
[html] view plain copy
  1. load data local inpath "/home/master/mycode/new_movies_load.csv" into table movie_message;  

    2):查看array的元素

      用下标进行寻找,类似于其他编程语言中的数组访问

[html] view plain copy
  1. hive> select leixing[0] from movie_message limit 5;  
  2. OK  
  3. 剧情  
  4. 剧情  
  5. 剧情  
  6. 纪录片  
  7. 喜剧  
  8. Time taken: 1.116 seconds, Fetched: 5 row(s)  

   3):内嵌查询及统计

     这里就是 写在前边的话中提到的问题,这里使用explode和lateral view关键字,应该这样写

[html] view plain copy
  1. select lx,count(*) from movie_message lateral view explode(leixing) leixing as lx group by lx;  
       结果为:
[html] view plain copy
  1. 传记    194  
  2. 儿童    18  
  3. 冒险    242  
  4. 剧情    1490  
  5. 动作    252  
  6. 动画    106  
  7. 历史    208  
  8. 古装    9  
  9. 同性    84  
  10. 喜剧    618  
  11. 奇幻    178  
  12. 家庭    130  
  13. 恐怖    152  
  14. 悬念    2  
  15. 悬疑    386  
  16. 情色    19  
  17. 惊悚    435  
  18. 戏曲    11  
  19. 战争    144  
  20. 歌舞    40  
  21. 武侠    1  
  22. 灾难    11  
  23. 爱情    404  
  24. 犯罪    442  
  25. 真人秀  6  
  26. 短片    165  
  27. 科幻    165  
  28. 纪录片  620  
  29. 脱口秀  10  
  30. 舞台艺术        8  
  31. 西部    6  
  32. 运动    29  
  33. 音乐    123  
  34. 鬼怪    1  
  35. 黑色电影        4  

     4):如何保存查询结果

       这里使用overwrite方法,只需在你的语句前加上即可

[html] view plain copy
  1. insert overwrite local directory "you path"  
          也可以指定字段之间的分隔符
[html] view plain copy
  1. row format delimited fields terminated by "\t"  
          还是上边统计类型的例子,这里将其查询结果保存在本地/home/master/mycode/movie_leixing
[html] view plain copy
  1. insert overwrite local directory "/home/master/mycode/movie_leixing"  
  2. row format delimited fields terminated by "\t"  
  3. select lx,count(*) from movie_message lateral view explode(leixing) leixing as lx group by lx;  
          

2:Map

     就是<key:value>这样的键值对,假设我们有这样格式的数据人物A,主演了BCD电影,将于2016-05上映

[html] view plain copy
  1. A       ABC:2016-05,EFG:2016-09  
  2. B       OPQ:2015-06,XYZ:2016-04  

     1):创建表

[html] view plain copy
  1. create table people_movie(  
  2. name string,  
  3. movie map<string,string> )  
  4. row format delimited fields terminated by "\t"  
  5. collection items terminated by ","  
  6. map keys terminated by ":";  
           加载数据
[html] view plain copy
  1. load data local inpath "/home/master/map" into table people_movie;  

     2):普通查看表数据

[html] view plain copy
  1. hive> select * from people_movie;  
  2. OK  
  3. A       {"ABC":"2016-05","EFG":"2016-09"}  
  4. B       {"OPQ":"2015-06","XYZ":"2016-04"}  
  5. A       {"ABC":"2016-05","EFG":"2016-09"}  
  6. B       {"OPQ":"2015-06","XYZ":"2016-04"}  
  7. Time taken: 0.148 seconds, Fetched: 4 row(s)  
  8. hive> select movie['ABC'] from people_movie;  
  9. OK  
  10. 2016-05  
  11. NULL  
  12. 2016-05  
  13. NULL  
  14. Time taken: 0.144 seconds, Fetched: 4 row(s)  

    3):使用explode关键字查询

[html] view plain copy
  1. hive> select explode(movie) as (m_name,m_time) from people_movie;  
  2. OK  
  3. ABC     2016-05  
  4. EFG     2016-09  
  5. OPQ     2015-06  
  6. XYZ     2016-04  
  7. ABC     2016-05  
  8. EFG     2016-09  
  9. OPQ     2015-06  
  10. XYZ     2016-04  
  11. Time taken: 0.121 seconds, Fetched: 8 row(s)  

   4):使用explode和lateral view结合查询

[html] view plain copy
  1. hive> select name,mo,time from people_movie lateral view explode(movie) movie as mo,time;   
  2. OK  
  3. A       ABC     2016-05  
  4. A       EFG     2016-09  
  5. B       OPQ     2015-06  
  6. B       XYZ     2016-04  
  7. A       ABC     2016-05  
  8. A       EFG     2016-09  
  9. B       OPQ     2015-06  
  10. B       XYZ     2016-04  
  11. Time taken: 0.147 seconds, Fetched: 8 row(s)  

3:Structs

     类似于C语言中的结构体,内部数据通过X.X来获取,假设我们的数据格式是这样的,电影ABC,有1254人评价过,打分为7.4分

[html] view plain copy
  1. ABC     1254:7.4  
  2. DEF     256:4.9  
  3. XYZ     456:5.4  

     1):创建数据表

[html] view plain copy
  1. Time taken: 0.147 seconds, Fetched: 8 row(s)  
  2. hive> create table movie_score(  
  3.     > name string,  
  4.     > info struct<number:int,score:float>  
  5.     > )row format delimited fields terminated by "\t"  
  6.     > collection items terminated by ":";  

     2):查询表数据

[html] view plain copy
  1. hive> select * from movie_score;  
  2. OK  
  3. ABC     {"number":1254,"score":7.4}  
  4. DEF     {"number":256,"score":4.9}  
  5. XYZ     {"number":456,"score":5.4}  
  6. Time taken: 0.103 seconds, Fetched: 3 row(s)  
  7. hive> select info.number,info.score from movie_score;  
  8. OK  
  9. 1254    7.4  
  10. 256     4.9  
  11. 456     5.4  
  12. Time taken: 0.148 seconds, Fetched: 3 row(s)  

4:collect_set函数

     这里再另外介绍一个函数collect_set(),该函数的作用是将某字段的值进行去重汇总,产生Array类型字段,假设数据格式如下:

[html] view plain copy
  1. hive> select * from test;  
  2. OK  
  3. 1       A  
  4. 1       C  
  5. 1       B  
  6. 2       B  
  7. 2       C  
  8. 2       D  
  9. 3       B  
  10. 3       C  
  11. 3       D  
  12. Time taken: 0.096 seconds, Fetched: 6 row(s)  
      现在要统计每个id得到的等级
[html] view plain copy
  1. select id,collect_set(name) from test group by id;  
      结果为
[html] view plain copy
  1. Total MapReduce CPU Time Spent: 3 seconds 360 msec  
  2. OK  
  3. 1       ["A","C","B"]  
  4. 2       ["B","C","D"]  
  5. 3       ["B","C","D"]  
  6. Time taken: 32.298 seconds, Fetched: 3 row(s)

原创粉丝点击