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行记录。

posted @ 2022-12-05 17:47  硅谷工具人  阅读(1138)  评论(0编辑  收藏  举报
成功之道,在于每个人生阶段都要有不同的目标,并且通过努力实现自己的目标,毕竟人生不过百年! 所有奋斗的意义在于为个人目标实现和提升家庭幸福,同时能推进社会进步和国家目标! 正如古人讲的正心诚意格物致知,修身齐家治国平天下。