Hive Lateral view介绍

1). Lateral View语法

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

2). Lateral View用于UDTF(user-defined table generating functions)中将行转成列,例如explode().

3). 目前Lateral View不支持有上而下的优化。如果使用Where子句,查询可能将不被编译。解决方法见:

此时,在查询之前执行et hive.optimize.ppd=false;

4).例子

pageAds。它有两个列:pageid,adid_list:

string pageidArray<int> adid_list
"front_page"[1, 2, 3]
"contact_page"[3, 4, 5]

SELECT pageid, adid FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;

将输出如下结果:

string pageidint adid
"front_page"1
"front_page"2
"front_page"3
"contact_page"3
"contact_page"4
"contact_page"

count/group可以被使用:

SELECT adid, count(1) FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid GROUP BY adid;

结果如下:

int adidcount(1)
11
21
32
41
51

5). 多个Lateral View

例如下面的HQL:

SELECT * FROM exampleTable LATERAL VIEW explode(col1) myTable1 AS myCol1 LATERAL VIEW explode(myCol1) myTable2 AS myCol2;

假设使用的base表如下:

Array<int> col1Array<string> col2
[1, 2][a", "b", "c"]
[3, 4][d", "e", "f"]

HQL:

SELECT myCol1, col2 FROM baseTable LATERAL VIEW explode(col1) myTable1 AS myCol1;

将产生如下结果:

int mycol1Array<string> col2
1[a", "b", "c"]
2[a", "b", "c"]
3[d", "e", "f"]
4[d", "e", "f"]

多个Lateral View的HQL:

SELECT myCol1, myCol2 FROM baseTable LATERAL VIEW explode(col1) myTable1 AS myCol1
LATERAL VIEW explode(col2) myTable2 AS myCol2;

将产生如下结果:

int myCol1string 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"

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值