Hive Lateral View

目录

Hive explode介绍:

Hive posexplode介绍:

Lateral View介绍:

使用多个Lateral View:

outer关键字:


Hive explode介绍:

它会将一行数据转成多行,也就是将行数据转成列数据,它所接收的参数为array和map类型的数据,语法如下:

array参数:

select explode(array) as arr_name from tableName;

map参数

select explode(map) as (map_key,map_value) from tableName;

Hive posexplode介绍:

与explode不同的是他会多生成一列索引值,从0开始。

Lateral View介绍:

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.

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

这个虚拟表会和输入的行进行join,达到连接UDTF外的select字段的目的。

如下:

Hive Lateral View_第1张图片

Hive Lateral View_第2张图片

也可以使用posexplode:

Hive Lateral View_第3张图片

也可以再进一步进行聚合:

使用多个Lateral View:

将上面表的page_id也改为多个用逗号隔开的形式,如下

Hive Lateral View_第4张图片

将page_id和addid_list都拆分开:

Hive Lateral View_第5张图片

如果想要不要重复数据,也可以使用posexplode,然后加一个where条件,上面的数据不太合适,但是效果是相同的,几front_page-1给1,front_page-2给2,contact_page-1给4,contact_page-2给5:

其实我们发现两个列拆分后就是进行了一个全连接。

outer关键字:

UDTF默认是忽略输出空的,outer关键字的作用是在UDTF转换列的时候将其中的空也给展示出来,显示为NULL,如下:

Hive Lateral View_第6张图片

你可能感兴趣的:(大数据知识,数据库,hive,Lateral,View)