其他分享
首页 > 其他分享> > 第八章_函数【explode、lateral view、列转行】

第八章_函数【explode、lateral view、列转行】

作者:互联网

1. explode (遍历集合 UDTF函数)
说明 : 将hive 中array 数据遍历成多行, map 遍历成 多行、多列(key,value)
注意 : 1. udtf 只支持 select 子语句中有一个表达式
2. explode() takes an array or a map as a parameter 只能接受一个 array或者map的参数
示例 :
-- 1. array
select explode(split('da,wang,lai,le',','));
-- 2. map
maptab.name maptab.friend
刘备 {"关羽":120,"山西":null}
曹操 {"许褚":20}
孙权 {"吕蒙":null}
-- 执行sql
select explode(friend) from maptab;
-- 结果
key value
关羽 120
山西 NULL
许褚 20
吕蒙 NULL

2. lateral view (侧视图)
1. 语法 : lateral view udtf(expression) tablealias as columnalias
2. 说明 : 将 udtf函数生成的多行数据,作为一个表(视图)使用

3. 示例(列转行) :
-- 人员表
select * from arraytab;
arraytab.name arraytab.friends
刘备1 ["关羽","张飞","马超","诸葛亮","黄忠","赵云"]
曹操1 ["许褚","荀彧","司马懿"]
-- 操作sql
select name,friend from arraytab
lateral view explode(friends) t1 as friend;
-- 结果
name friend
刘备1 关羽
刘备1 张飞
刘备1 马超
刘备1 诸葛亮
刘备1 黄忠
刘备1 赵云
曹操1 许褚
曹操1 荀彧
曹操1 司马懿

标签:--,lateral,刘备,explode,friend,array,select,view
来源: https://www.cnblogs.com/bajiaotai/p/15864506.html