• Lateral View和UDTF类功能函数一起使用,表中的每一行和UDTF函数输出的每一行进行连接,生成一张新的虚拟表,可以对UDTF产生的记录设置字段名称,新加的字段可以使用在sort by,group by等语句中,不需要再套一层子查询。Lateral View的作用是可以扩展原来的表数据。
  • Lateral View Syntax:
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
fromClause: FROM baseTable (lateralView)*
  • 示例准备
# 创建表
create table lateral_test(
name string,
course string,
hobby string
)
row format delimited
fields terminated by '\t'
lines terminated by '\n'; # 样例数据
[root@wadeyu ~]# cat lateral_test.log
wadeyu c1,c2,c3 basketball,tennis
tom c2,c3,c5 swimming,trip
polly c1,c2,c3 sz # 查询数据
0: jdbc:hive2://> select * from lateral_test;
OK
+--------------------+----------------------+---------------------+--+
| lateral_test.name | lateral_test.course | lateral_test.hobby |
+--------------------+----------------------+---------------------+--+
| wadeyu | c1,c2,c3 | basketball,tennis |
| tom | c2,c3,c5 | swimming,trip |
| polly | c1,c2,c3 | sz |
+--------------------+----------------------+---------------------+--+
3 rows selected (0.511 seconds)
  • 示例一:单个Lateral View
select
name,
course,
t_hobby.hobby
from lateral_test
lateral view explode(split(hobby, ',')) t_hobby as hobby; OK
+---------+-----------+----------------+--+
| name | course | t_hobby.hobby |
+---------+-----------+----------------+--+
| wadeyu | c1,c2,c3 | basketball |
| wadeyu | c1,c2,c3 | tennis |
| tom | c2,c3,c5 | swimming |
| tom | c2,c3,c5 | trip |
| polly | c1,c2,c3 | sz |
+---------+-----------+----------------+--+
5 rows selected (0.535 seconds)
  • 示例二:多个Lateral View

    • Lateral View左边的字段都可以使用
select
name,
t_course.course,
t_hobby.hobby
from lateral_test
lateral view explode(split(course, ',')) t_course as course
lateral view explode(split(hobby, ',')) t_hobby as hobby; +---------+------------------+----------------+--+
| name | t_course.course | t_hobby.hobby |
+---------+------------------+----------------+--+
| wadeyu | c1 | basketball |
| wadeyu | c1 | tennis |
| wadeyu | c2 | basketball |
| wadeyu | c2 | tennis |
| wadeyu | c3 | basketball |
| wadeyu | c3 | tennis |
| tom | c2 | swimming |
| tom | c2 | trip |
| tom | c3 | swimming |
| tom | c3 | trip |
| tom | c5 | swimming |
| tom | c5 | trip |
| polly | c1 | sz |
| polly | c2 | sz |
| polly | c3 | sz |
+---------+------------------+----------------+--+
15 rows selected (0.521 seconds)
  • 示例三:Outer Lateral View

    • UDTF未产生行时,连接结果为空,如果想输入的行也输出,需要加上Outer关键字
# 未加Outer关键字,输出内容为空
select
name,
course,
col1
from lateral_test
lateral view explode(array()) et as col1; +-------+---------+-------+--+
| name | course | col1 |
+-------+---------+-------+--+
+-------+---------+-------+--+
No rows selected (0.499 seconds) # 加上Outer关键字,输出不为空
select
name,
course,
col1
from lateral_test
lateral view outer explode(array()) et as col1; +---------+-----------+-------+--+
| name | course | col1 |
+---------+-----------+-------+--+
| wadeyu | c1,c2,c3 | NULL |
| tom | c2,c3,c5 | NULL |
| polly | c1,c2,c3 | NULL |
+---------+-----------+-------+--+
3 rows selected (0.543 seconds)

参考资料

【0】Hive wiki - LanguageManual LateralView

Hive之侧视图(Lateral View)的更多相关文章

  1. hive中的lateral view 与 explode函数的使用

    hive中的lateral view 与 explode函数的使用 背景介绍: explode与lateral view在关系型数据库中本身是不该出现的. 因为他的出现本身就是在操作不满足第一范式的数 ...

  2. hive中,lateral view 与 explode函数

    hive中常规处理json数据,array类型json用get_json_object(#,"$.#")这个方法足够了,map类型复合型json就需要通过数据处理才能解析. exp ...

  3. hive splict, explode, lateral view, concat_ws

    hive> create table arrays (x array<string>) > row format delimited fields terminated by ...

  4. hive中的 lateral view

    lateral view用于和split, explode等UDTF一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合. 一个简单的例子,假设我们有一张表pageAds,它有 ...

  5. hive lateral view 与 explode详解

    ref:https://blog.csdn.net/bitcarmanlee/article/details/51926530 1.explode hive wiki对于expolde的解释如下: e ...

  6. lateral view

    原文地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LateralView# lateral view用于和spl ...

  7. 【hive】lateral view的使用

    当使用UDTF函数的时候,hive只允许对拆分字段进行访问的 例如: select id,explode(arry1) from table; —错误 会报错FAILED: SemanticExcep ...

  8. hive 使用笔记(table format;lateral view)

    1. create table 创建一张目标表,指定分隔符和存储格式: create table tmp_2 (resource_id bigint ,v int) ROW FORMAT DELIMI ...

  9. 【Hive学习之六】Hive Lateral View &视图&索引

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-3.1.1 ...

随机推荐

  1. php中 mysql 插入特殊字符(手机端的emoji表情)出现异常

    今天在用mysql存储从微信服务器拉来的数据,出现插入数据异常,报 Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F 的错误. 最终在网上查了一下,有 ...

  2. 详解Android Activity生命周期

    转载注明来自:  http://www.cnblogs.com/wujiancheng/ 一.正常情况下Activity的生命周期:  Activity的生命周期大概可以归为三部分 整个的生命周期:o ...

  3. R Programming week1-Reading Data

    Reading Data There are a few principal functions reading data into R. read.table, read.csv, for read ...

  4. linux下redis安装访问

    下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...

  5. CentOS 6.4 linux下编译安装 LNMP环境

    1.nginx编译安装 2.PHP编译安装 3.mysql编译安装 4.NGINX配置模板 5.CentOS 6.4 php-fpm 添加service 添加平滑启动/重启

  6. CSS3 radio 或checkbox 自定义 样式

    .style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...

  7. samba的安装及其使用

    参考pdf文档:https://pan.baidu.com/s/1iPJ1iPtNx7ZXNHRurrjfqw centos7 samba配置#centos7安装Sambayum -y install ...

  8. vue 模板下只能有一个跟节点 根节点一定要是个div

    <template> <div>简单说就是里面只能有一个跟的div button1.vue <template> <div> <Button> ...

  9. CAD参数绘制实心圆弧填充(com接口)

    C#中实现代码说明: private void DrawPathToHatch1() { //把路径的开始位置移动指定的点 //参数一为点的X坐标 ,参数二为点的Y坐标,参数三为该点处开始宽度,对Po ...

  10. 单文件组件.vue---父子组件通信

    每一个.vue 文件就是一个 组件,组件和组件相互组合,就成了一个应用,这就涉及到的组件和组件之间的通信,最常用的就是父子之间的通信.在vue 中, 在一个组件中通过 import 引入另一个组件,这 ...