[HIve - LanguageManual] LateralView
Lateral View Syntax
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*fromClause: FROM baseTable (lateralView)* |
Description
Lateral view is used in conjunction with user-defined table generating functions such as explode(). As mentioned in Built-in Table-Generating Functions, a UDTF generates zero or more output rows for each input row. A lateral view first applies the UDTF to each row of base table and then joins resulting output rows to the input rows to form a virtual table having the supplied table alias.
Version
Icon
Prior to Hive 0.6.0, lateral view did not support the predicate push-down optimization. In Hive 0.5.0 and earlier, if you used a WHERE clause your query may not have compiled. A workaround was to add set hive.optimize.ppd=false; before your query. The fix was made in Hive 0.6.0; seehttps://issues.apache.org/jira/browse/HIVE-1056: Predicate push down does not work with UDTF's.
Version
Icon
From Hive 0.12.0, column aliases can be omitted. In this case, aliases are inherited from field names of StructObjectInspector which is returned from UTDF.
Example
Consider the following base table named pageAds. It has two columns: pageid (name of the page) and adid_list (an array of ads appearing on the page):
|
Column name |
Column type |
|---|---|
|
pageid |
STRING |
|
adid_list |
Array<int> |
An example table with two rows:
|
pageid |
adid_list |
|---|---|
|
front_page |
[1, 2, 3] |
|
contact_page |
[3, 4, 5] |
and the user would like to count the total number of times an ad appears across all pages.
A lateral view with explode() can be used to convert adid_list into separate rows using the query:
SELECT pageid, adidFROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid; |
The resulting output will be
|
pageid (string) |
adid (int) |
|---|---|
|
"front_page" |
1 |
|
"front_page" |
2 |
|
"front_page" |
3 |
|
"contact_page" |
3 |
|
"contact_page" |
4 |
|
"contact_page" |
5 |
Then in order to count the number of times a particular ad appears, count/group by can be used:
SELECT adid, count(1)FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adidGROUP BY adid; |
|
int adid |
count(1) |
|
1 |
1 |
|
2 |
1 |
|
3 |
2 |
|
4 |
1 |
|
5 |
1 |
Multiple Lateral Views
A FROM clause can have multiple LATERAL VIEW clauses. Subsequent LATERAL VIEWS can reference columns from any of the tables appearing to the left of the LATERAL VIEW.
For example, the following could be a valid query:
SELECT * FROM exampleTableLATERAL VIEW explode(col1) myTable1 AS myCol1LATERAL VIEW explode(myCol1) myTable2 AS myCol2; |
LATERAL VIEW clauses are applied in the order that they appear. For example with the following base table:
|
Array<int> col1 |
Array<string> col2 |
|
[1, 2] |
[a", "b", "c"] |
|
[3, 4] |
[d", "e", "f"] |
The query:
SELECT myCol1, col2 FROM baseTableLATERAL VIEW explode(col1) myTable1 AS myCol1; |
Will produce:
|
int mycol1 |
Array<string> col2 |
|
1 |
[a", "b", "c"] |
|
2 |
[a", "b", "c"] |
|
3 |
[d", "e", "f"] |
|
4 |
[d", "e", "f"] |
A query that adds an additional LATERAL VIEW:
SELECT myCol1, myCol2 FROM baseTableLATERAL VIEW explode(col1) myTable1 AS myCol1LATERAL VIEW explode(col2) myTable2 AS myCol2; |
Will produce:
|
int myCol1 |
string myCol2 |
|
1 |
"a" |
|
1 |
"b" |
|
1 |
"c" |
|
2 |
"a" |
|
2 |
"b" |
|
2 |
"c" |
|
3 |
"d" |
|
3 |
"e" |
|
3 |
"f" |
|
4 |
"d" |
|
4 |
"e" |
|
4 |
"f" |
Outer Lateral Views
Version
Icon
Introduced in Hive version 0.12.0
The user can specify the optional OUTER keyword to generate rows even when a LATERAL VIEW usually would not generate a row. This happens when the UDTF used does not generate any rows which happens easily with explode when the column to explode is empty. In this case the source row would never appear in the results. OUTER can be used to prevent that and rows will be generated with NULL values in the columns coming from the UDTF.
For example, the following query returns an empty result:
SELEC * FROM src LATERAL VIEW explode(array()) C AS a limit 10; |
But with the OUTER keyword
SELECT * FROM src LATERAL VIEW OUTER explode(array()) C AS a limit 10; |
it will produce:
238 val_238 NULL
86 val_86 NULL
311 val_311 NULL
27 val_27 NULL
165 val_165 NULL
409 val_409 NULL
255 val_255 NULL
278 val_278 NULL
98 val_98 NULL
[HIve - LanguageManual] LateralView的更多相关文章
- [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)
Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...
- [Hive - LanguageManual ] Windowing and Analytics Functions (待)
LanguageManual WindowingAndAnalytics Skip to end of metadata Added by Lefty Leverenz, last edi ...
- [Hive - LanguageManual] Import/Export
LanguageManual ImportExport Skip to end of metadata Added by Carl Steinbach, last edited by Le ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
- [Hive - LanguageManual] Alter Table/Partition/Column
Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add ...
- Hive LanguageManual DDL
hive语法规则LanguageManual DDL SQL DML 和 DDL 数据操作语言 (DML) 和 数据定义语言 (DDL) 一.数据库 增删改都在文档里说得也很明白,不重复造车轮 二.表 ...
- [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization
Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...
- [Hive - LanguageManual] Hive Concurrency Model (待)
Hive Concurrency Model Hive Concurrency Model Use Cases Turn Off Concurrency Debugging Configuration ...
- [Hive - LanguageManual ] Explain (待)
EXPLAIN Syntax EXPLAIN Syntax Hive provides an EXPLAIN command that shows the execution plan for a q ...
随机推荐
- iOS 相机和相册使用授权
1.判断用户是否有权限访问相册 授权一次后,不在提示是否授权 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus a ...
- lua的split函数
function split(s, delim) then return end local t = {} while true do local pos = string.find (s, deli ...
- Spring两种实现AOP的方式
有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...
- PHP命名空间概念解析
1. PHP中的命名空间是什么? 什么是命名空间?“从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录用来将相关文件分组,对于目录中的文件来说,它就扮 ...
- linux下的共享库(动态库)和静态库
1.什么是库在windows平台和linux平台下都大量存在着库.本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和linux的本质不同,因此二者库的二进制是不 ...
- Unrecognized selector sent to instance xxxxxxx
两个界面传递参数时报这个错误,经检查发现,是因为目标视图没有关联对应的controller.
- [HZAU]华中农业大学第四届程序设计大赛网络同步赛
听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...
- fzu Problem 2148 Moon Game(几何 凸四多边形 叉积)
题目:http://acm.fzu.edu.cn/problem.php?pid=2148 题意:给出n个点,判断可以组成多少个凸四边形. 思路: 因为n很小,所以直接暴力,判断是否为凸四边形的方法是 ...
- 待实践二:MVC3下的3种验证 (1)前台 jquery validate验证 (2)MVC实体验证 (3)EF生成的/自己手写的 自定义实体校验(伙伴类+元素据共享)
MVC3下的3种验证 (1):前台Jquery Validate脚本验证 引入脚本 <script src="../js/jquery.js" type="text ...
- web请求报出 “超过了最大请求长度” 【注意:重启IIS】
摘自:http://www.cnblogs.com/loalongblogs/archive/2012/10/16/2726372.html web请求报出 “超过了最大请求长度” 错误原因:as ...