Presto如何把Json陣列分解為不同的列
阿新 • • 發佈:2020-12-21
技術標籤:Presto SQLprestojson
Presto如何把Json陣列分解為多行資料
Cross Join Unnest
json格式如下:
{
"attendances":[
{
"attendanceToken":"e333e39b-033a-4db3-9c33-ed777b1ea6fb",
"userMeta":"{}",
"roleCode" :"participant",
"enterTime":1604490880630,
"exitTime":1604492850267,
"userDevice":"tablet",
"externalUserId":"41438910",
"attendanceRefCode":"5496dc8b-05aa-45bd-b3a4-0062a822803c" ,
"displayName":"Lisa Guo"
},
{
"attendanceToken":"a7cdd08c-ab17-47f3-99d6-e175887511aa",
"userMeta":"{}",
"roleCode":"host",
"enterTime" :1604491369673,
"exitTime":1604491446190,
"userDevice":"desktop",
"externalUserId":"44e3bfc0-4924-4404-b77b-f2d066a15ec6",
"attendanceRefCode":"fef1f257-4cdc-4805-b60b-65147195af2f",
"displayName":"Joseph V."
},
{
"attendanceToken":"a7cdd08c-ab17-47f3-99d6-e175887511aa",
"userMeta":"{}",
"roleCode":"host",
"enterTime":1604491394440,
"exitTime":1604492898877,
"userDevice":"desktop",
"externalUserId":"44e3bfc0-4924-4404-b77b-f2d066a15ec6",
"attendanceRefCode":"fef1f257-4cdc-4805-b60b-65147195af2f",
"displayName":"Joseph V."
}
]
}
程式碼為:
// An highlighted block
select
name,
attendance['attendanceToken'] as attedanceToken,
attendance['userMeta'] as userMeta,
attendance['roleCode'] as roleCode,
from_unixtime(cast(attendance['enterTime'] as BIGINT)/1000, 'Asia/Shanghai') as enterTime,
from_unixtime(cast(attendance['exitTime'] as BIGINT)/1000, 'Asia/Shanghai') as exitTime,
attendance['userDevice'] as userDevice,
attendance['externalUserId'] as externalUserId,
attendance['attendanceRefCode'] as attendanceRefCode,
attendance['displayName'] as displayName
from
(
select
r.name,
cast(json_extract(r.attendance_detail__c,
'$.attendances') as array<map<VARCHAR,
VARCHAR>>) attendances
from
reservation r)
cross join unnest (attendances) as attendances(attendance)
結果如下: