hive和trino中的爆炸函数lateral view explode与cross join unnest用法

遇到一个不规则的json如下:image

trace是数组,外面2个time,hash都是单个字段。
所以通过hive建表如下,trace使用了array包裹了struct结构:

create external table xy_ods.ods_address_trace(
trace array<struct<action:string,blockHash:string,blockNumber:bigint,result:string,subtraces:string,traceAddress:string,transactionHash:string,transactionPosition:bigint,type:string>>,
`time` bigint comment '事件时间',
hash string comment 'hash'
)  comment 'trace'
partitioned BY (pk_year string,pk_month string,pk_day string)
row format serde 'org.openx.data.jsonserde.JsonSerDe'
stored as textfile;

image
这里找到其中一条数据,trace对应的有16条记录。

那么在hive中取数时,想将trace数组一行转多行。那么使用炸裂函数如下:

select hash,`time`,a.type,a.blockhash,a.action,a.blocknumber,a.subtraces,a.traceAddress,a.transactionHash
from xy_ods.ods_address_trace
lateral view explode(trace) tmp as a
where hash = '0x1538f4267cf97b293a08d1ee9d61134b9c5acbce21addb72c05b2942dc86eb85'

image
结果展开为16行记录。

如果在trino中查询,则使用如下方法:

select hash,"time",u.blockhash,u.action,u.blocknumber
from hive.xy_ods.ods_address_trace a
cross join unnest(trace) as u
where hash = '0x1538f4267cf97b293a08d1ee9d61134b9c5acbce21addb72c05b2942dc86eb85'

image

同样也是一行展开为16行记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

硅谷工具人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值