首页 > 其他分享 >hive和trino中的爆炸函数lateral view explode与cross join unnest用法

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

时间:2022-12-05 17:55:56浏览次数:67  
标签:hash string trace lateral ods cross hive time join

遇到一个不规则的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行记录。

标签:hash,string,trace,lateral,ods,cross,hive,time,join
From: https://www.cnblogs.com/30go/p/16953004.html

相关文章