Hive之explode()函数和posexplode()函数和lateral view函数

本文详细介绍了Hive中用于数据拆分与转换的explode、posexplode及lateralview函数。explode函数将数组或映射结构拆分为单独的行,而posexplode不仅返回元素,还附加了索引,适合多列转换。lateralview则用于配合UDTF,将一列数据拆分成多行并形成虚拟表。通过实例展示了如何使用这些函数实现数据的正确匹配和转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、explode()函数

英文释义:
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.

As an example of using explode() in the SELECT expression list, consider a table named myTable that has a single column (myCol) and two rows:
总结:explode就是将hive一行中复杂的array或者map结构拆分成多行
举例:

hive>with temp  as
    ( select '2:00,3:00,4:00,5:00' as examp_data)

select
data
from temp
lateral view explode(split(examp_data,',')) view1 as data;
2:00
3:00
4:00
5:00

思考:现在是一列,如果是两列呢?

hive>with temp  as
    ( select 'a,b,c,d' as examp_data1,'2:00,3:00,4:00,5:00' as examp_data2)

select
data1
,data2
from temp
lateral view explode(split(examp_data1,',')) view1 as data1
lateral view explode(split(examp_data2,',')) view1 as data2
data1,data2
a,2:00
a,3:00
a,4:00
a,5:00
b,2:00
b,3:00
b,4:00
b,5:00
c,2:00
c,3:00
c,4:00
c,5:00
d,2:00
d,3:00
d,4:00
d,5:00

显然和我们想象的是有出入的,我们想让’a,b,c,d’和’2:00,3:00,4:00,5:00’一一对应该如何实现呢?

2、posexplode()函数

特点是不仅炸裂出数值,还附带索引,实现多列进行多行转换;
上述的例子可以通过posexplode()函数实现;
示例如下:

hive>with temp  as
    ( select 'a,b,c,d' as examp_data1,'2:00,3:00,4:00,5:00' as examp_data2)

select
data1
,data2
from temp
lateral view posexplode(split(examp_data1,',')) view1 as index1,data1
lateral view posexplode(split(examp_data2,',')) view1 as index2,data2
where index1=index2
data1,data2
a,2:00
b,3:00
c,4:00
d,5:00

3、lateral view函数

英文解释:
Lateral View Syntax
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (’,’ columnAlias)*
fromClause: FROM baseTable (lateralView)*

Description
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用于和split, explode等UDTF一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合;
1、Lateral View用于和UDTF函数(explode、split)结合来使用

2、首先通过UDTF函数拆分成多行,再将多行结果组合成一个支持别名的虚拟表

3、主要解决在select使用UDTF做查询过程中,查询只能包含单个UDTF,不能包含其他字段、以及多个UDTF的问题

4、语法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias (‘,’ columnAlias)
上面已举例,不在重复;

4、lateral view 和 lateral view outer 的区别

lateral view explode 炸裂的数组中如果存在null,则这一条数据舍弃;lateral view ourer explode 炸裂的数组中如果存在null,则这一条数据保留,炸裂的字段值填充为null ;

测试:

with user_view as (  --构造虚拟表
 select 'xiaohong' name, '中级|黄金' level,'15' num,'看电视,跳舞' favs 
 union all 
 select 'xiaohei' name,'中级|黄金' level,'25' num, null as   favs
)

select  
name 
,column1
,num  
,column2
from  user_view
lateral view explode (split(level,'\\|')) table1 as column1 
lateral view explode (split(favs,'\\,')) table2 as column2

结果如下:
在这里插入图片描述

with user_view as (  --构造虚拟表
 select 'xiaohong' name, '中级|黄金' level,'15' num,'看电视,跳舞' favs 
 union all 
 select 'xiaohei' name,'中级|黄金' level,'25' num, null as   favs
)

select  
name 
,column1
,num  
,column2
from  user_view
lateral view outer explode (split(level,'\\|')) table1 as column1 
lateral view outer explode (split(favs,'\\,')) table2 as column2

结果如下:
在这里插入图片描述

### Hive 中 `posexplode` 函数详解 #### 使用方法 `posexplode` 是 Hive 提供的一个非常有用的函数,主要用于处理数组或映射类型的复杂数据结构。该函数不仅可以展开这些集合类型的数据,还可以附加每个元素的位置索引。 语法如下: ```sql posexplode(array|map) ``` 此函数会返回两个字段:一个是位置索引(从0开始),另一个是对应的值[^1]。 #### 示例演示 考虑有一个包含产品名称及其价格列表的表 `products`,其中每一行记录了一个产品的多个不同时间的价格: 假设表结构如下: - `name`: STRING 类型,表示商品名。 - `prices`: ARRAY<DOUBLE> 或 MAP<STRING, DOUBLE> 类型,存储一系列历史价格。 下面是一个具体的 SQL 查询例子来展示如何使用 `posexplode` 展开 prices 数组并获取其索引对应的价格值: ```sql SELECT name, pos, price FROM products LATERAL VIEW posexplode(prices) exploded AS pos, price; ``` 这段代码的作用是从 `products` 表中读取每条记录,并通过 `LATERAL VIEW` 结合 `posexplode()` 将 prices 字段拆分成多行输出,同时保留原始的产品名字以及新产生的两列——pos price,分别代表原数组/映射里各成员的位置序号与其实际数值[^3]。 #### 解释说明 上述查询语句执行的结果将是把原来单个单元格内的数组或映射形式的数据项逐一分离成独立的行显示出来,而且对于每一个分离出来的项目都会额外增加一列用来指示它原本位于源数据集里的具体位次。这对于后续基于特定顺序的操作或是分析来说是非常有帮助的功能特性之一[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值