Hive-sql中的explode()函数和posexplode()函数

实现多列转多行
1、创建测试数据

create table if not exists wedw_tmp.test_explode_df(
     id STRING,
     time STRING
)
row format delimited fields terminated by '-' 
lines terminated by '\n';

在这里插入图片描述

insert overwrite table wedw_tmp.test_explode_df
select  
'a,b,c,d' as id,
'2:00,3:00,4:00,5:00' as `time`
union all
select  
'e,b,c,d' as id,
'1:20,2:00,3:30,4:40' as `time`

在这里插入图片描述
在这里插入图片描述
2、现在先进行一列转多行的操作,这里就用到了explode()函数,将第二列time中的数据用逗号切分并成为第三列,操作如下

select id,time,single_time 
from wedw_tmp.test_explode_df lateral view explode(split(time,',')) t as single_time

在这里插入图片描述

可以看出上面的代码实现了对第二列的多行转换,现在如果想实现对两列听同事进行多行转换,那么用explode()函数就不能实现了,但可以用posexplode()函数,因为该函数可以将index和数据都取出来,使用两次posexplode并令两次取到的index相等就行了。
3、使用一次posexplode()函数效果如下:

select id,time,single_id_index,single_id from wedw_tmp.test_explode_df lateral view posexplode(split(id,',')) t as single_id_index, single_id;

在这里插入图片描述
2、使用两次posexplode()函数实现多列转多行

select id,time,single_id_index ,single_time_index,single_id,single_time from wedw_tmp.test_explode_df 
lateral view posexplode(split(id,',')) t as single_id_index, single_id
lateral view posexplode(split(time,',')) t as single_time_index, single_time
where single_id_index = single_time_index;

在这里插入图片描述

结论:
explode()仅可以实现一列转行
posexplode()可实现多列转行

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有语忆语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值