当前位置:   article > 正文

Hive侧视图 Lateral View(单个、多个、outer)(附案例)_lateral view outer

lateral view outer

Lateral View 和UDTF类功能函数一起使用,表中的每一行和UDTF函数输出的每一行进行连接,生成一张新的虚拟表,可以对UDTF产生的记录设置字段名称,新加的字段可以使用在 sort by,group by 等语句中,不需要再套一层子查询。Lateral View 的作用是可以扩展原来的表数据。

Lateral View Syntax:

  1. lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
  2. fromClause: FROM baseTable (lateralView)*

案例数据准备:

  1. --创建表
  2. create table lateral_test(
  3. name string,
  4. course string,
  5. hobby string
  6. )
  7. row format delimited
  8. fields terminated by '\t'
  9. lines terminated by '\n';
  10. --样例数据
  11. [root@wadeyu ~]# cat lateral_test.log
  12. wadeyu c1,c2,c3 basketball,tennis
  13. tom c2,c3,c5 swimming,trip
  14. polly c1,c2,c3 sz
  15. --查询数据
  16. 0: jdbc:hive2://> select * from lateral_test;
  17. OK
  18. +--------------------+----------------------+---------------------+--+
  19. | lateral_test.name | lateral_test.course | lateral_test.hobby |
  20. +--------------------+----------------------+---------------------+--+
  21. | wadeyu | c1,c2,c3 | basketball,tennis |
  22. | tom | c2,c3,c5 | swimming,trip |
  23. | polly | c1,c2,c3 | sz |
  24. +--------------------+----------------------+---------------------+--+
  25. 3 rows selected (0.511 seconds)

示例一:单个 Lateral View

  1. select
  2. name,
  3. course,
  4. t_hobby.hobby
  5. from lateral_test
  6. lateral view explode(split(hobby, ',')) t_hobby as hobby;
  7. OK
  8. +---------+-----------+----------------+--+
  9. | name | course | t_hobby.hobby |
  10. +---------+-----------+----------------+--+
  11. | wadeyu | c1,c2,c3 | basketball |
  12. | wadeyu | c1,c2,c3 | tennis |
  13. | tom | c2,c3,c5 | swimming |
  14. | tom | c2,c3,c5 | trip |
  15. | polly | c1,c2,c3 | sz |
  16. +---------+-----------+----------------+--+
  17. 5 rows selected (0.535 seconds)

示例二:多个 Lateral View

  • Lateral View左边的字段都可以使用
  1. select
  2. name,
  3. t_course.course,
  4. t_hobby.hobby
  5. from lateral_test
  6. lateral view explode(split(course, ',')) t_course as course
  7. lateral view explode(split(hobby, ',')) t_hobby as hobby;
  8. +---------+------------------+----------------+--+
  9. | name | t_course.course | t_hobby.hobby |
  10. +---------+------------------+----------------+--+
  11. | wadeyu | c1 | basketball |
  12. | wadeyu | c1 | tennis |
  13. | wadeyu | c2 | basketball |
  14. | wadeyu | c2 | tennis |
  15. | wadeyu | c3 | basketball |
  16. | wadeyu | c3 | tennis |
  17. | tom | c2 | swimming |
  18. | tom | c2 | trip |
  19. | tom | c3 | swimming |
  20. | tom | c3 | trip |
  21. | tom | c5 | swimming |
  22. | tom | c5 | trip |
  23. | polly | c1 | sz |
  24. | polly | c2 | sz |
  25. | polly | c3 | sz |
  26. +---------+------------------+----------------+--+
  27. 15 rows selected (0.521 seconds)

示例三:Outer Lateral View

  • UDTF未产生行时,连接结果为空,如果想输入的行也输出,需要加上Outer关键字
  1. # 未加Outer关键字,输出内容为空
  2. select
  3. name,
  4. course,
  5. col1
  6. from lateral_test
  7. lateral view explode(array()) et as col1;
  8. +-------+---------+-------+--+
  9. | name | course | col1 |
  10. +-------+---------+-------+--+
  11. +-------+---------+-------+--+
  12. No rows selected (0.499 seconds)
  13. # 加上Outer关键字,输出不为空
  14. select
  15. name,
  16. course,
  17. col1
  18. from lateral_test
  19. lateral view outer explode(array()) et as col1;
  20. +---------+-----------+-------+--+
  21. | name | course | col1 |
  22. +---------+-----------+-------+--+
  23. | wadeyu | c1,c2,c3 | NULL |
  24. | tom | c2,c3,c5 | NULL |
  25. | polly | c1,c2,c3 | NULL |
  26. +---------+-----------+-------+--+
  27. 3 rows selected (0.543 seconds)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/725828
推荐阅读
相关标签
  

闽ICP备14008679号