Hive lateral view 与 explode
admin
2023-01-31 13:26:59
0

explode(官网链接

    explode 是一个 UDTF(表生成函数),将单个输入行转换为多个输出行。一般和 lateral view 结合使用,主要有两种用法:

输入类型

使用方法

描述

T

explode(ARRAY a)

将数组分解为多行,返回单列多行,每一行代表数组的一个元素

Tkey,Tvalue

explode(MAPkey,Tvalue> m)

将 MAP 分解为多行,返回的行具有两列(键-值),每一行代表输入中的一个键值对

示例

explode(array)

hive (default)> select explode(array('A','B','C'));
OK
col
A
B
C
Time taken: 0.402 seconds, Fetched: 3 row(s)
hive (default)> select explode(array('A','B','C')) as col1;
OK
col1
A
B
C
Time taken: 0.145 seconds, Fetched: 3 row(s)
hive (default)> select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf;
OK
tf.col
A
B
C
Time taken: 0.191 seconds, Fetched: 3 row(s)
hive (default)> select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf as col1;
OK
tf.col1
A
B
C

explode(map)

hive (default)> select explode(map('A',10,'B',20,'C',30));
OK
key    value
A    10
B    20
C    30
Time taken: 0.153 seconds, Fetched: 3 row(s)
hive (default)> select explode(map('A',10,'B',20,'C',30)) as (my_key,my_value);
OK
my_key    my_value
A    10
B    20
C    30
Time taken: 0.137 seconds, Fetched: 3 row(s)
hive (default)> select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf;
OK
tf.key    tf.value
A    10
B    20
C    30
Time taken: 0.128 seconds, Fetched: 3 row(s)
hive (default)> select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf as my_key,my_value;
OK
tf.my_key    tf.my_value
A    10
B    20
C    30
Time taken: 0.109 seconds, Fetched: 3 row(s)

LateralView(官网链接)

语法

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

fromClause: FROM baseTable (lateralView)*

描述

    lateralview 与用户自定义表生成函数(UDTF)(例如 explode)结合使用,UDTF 为每个输入行生成零个或多个输出行。lateralview 首先将 UDTF 应用于基础表的每一行,然后将结果输出行与输入行连接起来形成具有所提供表别名的虚拟表。

实例

    基础表 pageads 具有两列:pageid(页面名称)和 add_list(页面上显示的广告数组)。

hive (test)> !cat pageads
           > ;
front_page    1,2,3
contact_page    3,4,5
hive (test)> create table pageads(
      > pageid string,
      > add_list array)
      > ROW FORMAT delimited
      > fields terminated by '\t'
      > collection items terminated by ','
      > lines terminated by '\n'
     > ;
OK
Time taken: 0.099 seconds
hive (test)> load data local inpath '/home/hadoop/pageads' into table pageads;
Loading data to table test.pageads
OK
Time taken: 0.331 seconds
hive (test)> select * from pageads;
OK
pageads.pageid    pageads.add_list
front_page    [1,2,3]
contact_page    [3,4,5]
Time taken: 0.106 seconds, Fetched: 2 row(s)
hive (test)> select pageid,addid from pageads lateral view explode(add_list) adTable as addid;
OK
pageid    addid
front_page    1
front_page    2
front_page    3
contact_page    3
contact_page    4
contact_page    5
Time taken: 0.105 seconds, Fetched: 6 row(s)
hive (test)> select addid,count(1) from pageads lateral view explode(add_list) adTable as addid group by addid;
.......
OK
addid    _c1
1    1
2    1
3    2
4    1
5    1

多个lateralview

    from 子句可以具有多个 lateralview 子句。后续的 lateralview 子句可以引用 lateralview 左侧表中的任何列。例如以下查询:

SELECT * FROM exampleTable

LATERAL VIEW explode(col1) myTable1 AS myCol1

LATERAL VIEW explode(myCol1) myTable2 AS myCol2;

    注意,lateralview 子句按其出现的顺序应用。

实例

hive (test)> !cat basetable;
1,2    a,b,c
3,4    d,e,f
hive (test)> create table basetable(
           > col1 array,
           > col2 array)
           > ROW FORMAT delimited
           > fields terminated by '\t'
           > collection items terminated by ','
           > lines terminated by '\n'
           > ;
OK
Time taken: 0.113 seconds
hive (test)> load data local inpath '/home/hadoop/basetable' into table basetable;
Loading data to table test.basetable
OK
Time taken: 0.329 seconds
hive (test)> select * from basetable;
OK
basetable.col1    basetable.col2
[1,2]    ["a","b","c"]
[3,4]    ["d","e","f"]
Time taken: 0.104 seconds, Fetched: 2 row(s)
hive (test)> SELECT myCol1, col2 FROM basetable
           > LATERAL VIEW explode(col1) myTable1 AS myCol1;
OK
mycol1    col2
1    ["a","b","c"]
2    ["a","b","c"]
3    ["d","e","f"]
4    ["d","e","f"]
Time taken: 0.089 seconds, Fetched: 4 row(s)
hive (test)> SELECT myCol1, myCol2 FROM baseTable
           > LATERAL VIEW explode(col1) myTable1 AS myCol1
           > LATERAL VIEW explode(col2) myTable2 AS myCol2;
OK
mycol1    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
Time taken: 0.093 seconds, Fetched: 12 row(s)
Outer Lateral Views
hive (test)> SELECT * FROM basetable LATERAL VIEW explode(array()) C AS a limit 10;
OK
basetable.col1    basetable.col2    c.a
Time taken: 0.063 seconds
hive (test)> SELECT * FROM basetable LATERAL VIEW OUTER explode(array()) C AS a limit 10;
OK
basetable.col1    basetable.col2    c.a
[1,2]    ["a","b","c"]    NULL
[3,4]    ["d","e","f"]    NULL
Time taken: 0.092 seconds, Fetched: 2 row(s)


相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...