Hive中explode()和posexplode()函数总结

创建test表结构如下:

在这里插入图片描述
在这里插入图片描述
#将数组中的每个元素拆分为单独的行,生成单列

select explode(student) from test;

在这里插入图片描述
#单列拆分

select class,e_student from test lateral view explode(student) table_a as e_student;

在这里插入图片描述
#单列拆分——编号引入posexplode

select class,id,e_student from test lateral view posexplode(student) table_a as id,e_student;

在这里插入图片描述
#单列拆分——通过编号+1来实现序号从1开始

select class,id+1 stuid,e_student from test lateral view posexplode(student) table_a as id,e_student;

在这里插入图片描述
#多列拆分——错误示范直接用exploded会产生笛卡尔积

select class,e_student,e_score from test lateral view explode(student) table_a as e_student lateral view explode(score) table_a as e_score;

在这里插入图片描述
#多列拆分——正确做法配合posexplode()

select class,e_student,e_score from test lateral view posexplode(student) table_a as stuid,e_student lateral view 
posexplode(score) table_a as sid,e_score where stuid=sid;

在这里插入图片描述
#开窗按班级班内排名

select class,e_student,e_score,rank() over(partition by class order by e_score desc) rk from test lateral view posexplode(student) table_a as stuid,e_student lateral view 
posexplode(score) table_a as sid,e_score where stuid=sid;

在这里插入图片描述

创建test2表结构如下:

在这里插入图片描述
在这里插入图片描述
#将映射的键值对拆分为两列,分别对应 key和value

select explode(score) as (course,e_score) from test2 where student='小A';

在这里插入图片描述
#将映射的键值对拆分

select student,course,e_score from test2 lateral view explode(score) table_a as course,e_score;

在这里插入图片描述
posexplode()能否对键值类(map)进行编号呢?

select student,id,course,e_score from test2 lateral view posexplode(score) table_a as id,course,e_score;

在这里插入图片描述

这个错误提示表明你在使用posexplode函数时没有提供数组类型的参数。posexplode函数用于将数组(array)类型的列拆分成多行,同时保留元素在数组中的位置索引。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值