一、Explode用法

  • hive wiki对于expolde的解释如下:

explode() takes in an array (or a map) as an input and outputs the elements of the array (map) as separate rows. UDTFs can be used in the SELECT expression list and as a part of LATERAL VIEW.

explode()接受一个数组(或一个map)作为输入,并将数组元素(map)作为单独的行输出。 UDTF可以在SELECT表达式列表中使用,也可以作为LATERAL VIEW的一部分使用。

使用如下图:

  • 将Map作为输入端

Lateral View和Explode用法简介

  • 将ArrayList作为输入端:

Lateral View和Explode用法简介

二、Lateral View用法

lateral view的意义是配合explode(或者其他的UDTF),一个语句生成把单行数据拆解成多行后的数据结果集。

首先准备一张表test,test表的数据结构如下

Lateral View和Explode用法简介

利用 lateral view explode 结合map的使用方式和结果如下:
select
    t.cola
    ,t.indexa
    ,tt.class_id
    ,tt.score
from test t
LATERAL VIEW explode(map('1',100,'2',200)) tt as class_id ,score
;

输出结果:

Lateral View和Explode用法简介

结果注解:

test本身只有4条记录, explode(map(‘1’,100,‘2’,200)) 本身只有2条记录,

上述sql的原理实质上是对2个结果集做了笛卡尔积。

实现形式如下:

select 

    t1.column1

    ,t1.column2

    ,…

    ,t2.column1

    ,t2.column2 

from tablea t1 

lateral view explode(…) t2 as column1,column2

;
利用 lateral view explode 结合array的使用方式和结果如下:
select

    t.cola

    ,t.indexa

    ,tt.class_id

from test t

lateral view explode(array(1,100)) tt as class_id

;

输出如下:

Lateral View和Explode用法简介

相关文章:

  • 2022-01-02
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2021-07-06
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2021-10-15
  • 2021-12-05
  • 2022-12-23
  • 2021-07-28
  • 2021-12-01
相关资源
相似解决方案