需要帮助来解析雪花中的内容

时间:2020-06-09 14:12:25

标签: sql json snowflake-cloud-data-platform snowflake-schema snowflake-task

[{“ legs”:[{“ points”:[{“ latitude”:29.76524,“ longitude”:-95.35406},{“ latitude”:30.74438,“ longitude”:-101.71203},{“ latitude” :30.74421,“经度”:-101.71247},{“纬度”:30.74404,“经度”:-101.71289},{“纬度”:30.74366,“经度”:-101.71374},{“纬度”:30.74346,“经度“:-101.71415},{”纬度“:30.74303,”经度“:-101.71495},{”纬度“:30.74274,”经度“:-101.71543},{”纬度“:30.74234,”经度“:-101.71606} ,{“纬度”:31.82985,“经度”:-102.34753},{“纬度”:31.8302,“经度”:-102.34597},{“纬度”:31.83029,“经度”:-102.34557},{“纬度” :31.83038,“经度”:-102.34526},{“纬度”:31.83051,“经度”:-102.3448},{“纬度”:31.83081,“经度”:-102.344},{“纬度”:31.83099,“经度“:-102.34356},{”纬度“:31.83113,”经度“:-102.34328},{”纬度“:31.83145,”经度“:-102.34271},{”纬度“:31.83174,”经度“:-102.34226} ,{“ la titude“:31.83207,” longitude“:-102.34181},{” latitude“:31.83267,” longitude“:-102.34109},{” latitude“:31.83317,” longitude“:-102.34053},{” latitude“:31.83359, “ longitude”:-102.34007},{“ latitude”:31.8339,“ longitude”:-102.33971},{“ latitude”:31.83499,“ longitude”:-102.33852},{“ latitude”:31.83547,“ longitude”:- 102.338},{“纬度”:31.83553,“经度”:-102.33793},{“纬度”:31.83685,“经度”:-102.33648},{“纬度”:31.83764,“经度”:-102.3356},{“纬度”:31.83838,“经度”:-102.33479},{“纬度”:31.84575,“经度”:-102.32666},{“纬度”:31.84603,“经度”:-102.32636},{“纬度”:31.84679, “ longitude”:-102.32551},{“ latitude”:31.84878,“ longitude”:-102.32333},{“ latitude”:31.85095,“ longitude”:-102.32094},{“ latitude”:31.85131,“ longitude”:- 102.32054},{“纬度”:31.85134,“经度”:-102.32044},{“纬度”:31.85259,“经度”:-102.31886},{“纬度”:31.85273,“经度”:-10 2.31859},{“纬度”:31.85462,“经度”:-102.3165},{“纬度”:31.85467,“经度”:-102.31644},{“纬度”:31.85489,“经度”:-102.3162},{“纬度”:31.85505,“经度”:-102.3164},{“纬度”:31.8552,“经度”:-102.3166},{“纬度”:31.85533,“经度”:-102.31677},{“纬度”:31.85506, “ longitude”:-102.31706},{“ latitude”:31.85655,“ longitude”:-102.32234},{“ latitude”:31.85851,“ longitude”:-102.32294}],“ summary”:{“ arrivalTime”:“ 2020 -06-04T01:22:22-05:00“,” departureTime“:” 2020-06-03T17:28:11-05:00“,” lengthInMeters“:863989,” trafficDelayInSeconds“:528,” travelTimeInSeconds“: 28451}}],“ sections”:[{“ endPointIndex”:5797,“ sectionType”:“ TRAVEL_MODE”,“ startPointIndex”:0,“ travelMode”:“ car”}],“ summary”:{“ arrivalTime”: “ 2020-06-04T01:22:22-05:00”,“ departureTime”:“ 2020-06-03T17:28:11-05:00”,“ lengthInMeters”:863989,“ trafficDelayInSeconds”:528,“ travelTimeInSeconds “:28451}}]

1 个答案:

答案 0 :(得分:1)

我会咨询文档,尤其是https://docs.snowflake.com/en/sql-reference/functions/flatten.htmlhttps://docs.snowflake.com/en/user-guide/semistructured-concepts.html

这将取决于您如何加载数据,但这是一个示例查询,可帮助您入门:

with tbl as (select parse_json($1)::variant list_json from values ('[ { "legs": [ { "points": [ { "latitude": 29.76524, "longitude": -95.35406 }, { "latitude": 30.74438, "longitude": -101.71203 }, { "latitude": 30.74421, "longitude": -101.71247 }, { "latitude": 30.74404, "longitude": -101.71289 }, { "latitude": 30.74366, "longitude": -101.71374 }, { "latitude": 30.74346, "longitude": -101.71415 }, { "latitude": 30.74303, "longitude": -101.71495 }, { "latitude": 30.74274, "longitude": -101.71543 }, { "latitude": 30.74234, "longitude": -101.71606 }, { "latitude": 31.82985, "longitude": -102.34753 }, { "latitude": 31.8302, "longitude": -102.34597 }, { "latitude": 31.83029, "longitude": -102.34557 }, { "latitude": 31.83038, "longitude": -102.34526 }, { "latitude": 31.83051, "longitude": -102.3448 }, { "latitude": 31.83081, "longitude": -102.344 }, { "latitude": 31.83099, "longitude": -102.34356 }, { "latitude": 31.83113, "longitude": -102.34328 }, { "latitude": 31.83145, "longitude": -102.34271 }, { "latitude": 31.83174, "longitude": -102.34226 }, { "latitude": 31.83207, "longitude": -102.34181 }, { "latitude": 31.83267, "longitude": -102.34109 }, { "latitude": 31.83317, "longitude": -102.34053 }, { "latitude": 31.83359, "longitude": -102.34007 }, { "latitude": 31.8339, "longitude": -102.33971 }, { "latitude": 31.83499, "longitude": -102.33852 }, { "latitude": 31.83547, "longitude": -102.338 }, { "latitude": 31.83553, "longitude": -102.33793 }, { "latitude": 31.83685, "longitude": -102.33648 }, { "latitude": 31.83764, "longitude": -102.3356 }, { "latitude": 31.83838, "longitude": -102.33479 }, { "latitude": 31.84575, "longitude": -102.32666 }, { "latitude": 31.84603, "longitude": -102.32636 }, { "latitude": 31.84679, "longitude": -102.32551 }, { "latitude": 31.84878, "longitude": -102.32333 }, { "latitude": 31.85095, "longitude": -102.32094 }, { "latitude": 31.85131, "longitude": -102.32054 }, { "latitude": 31.85134, "longitude": -102.32044 }, { "latitude": 31.85259, "longitude": -102.31886 }, { "latitude": 31.85273, "longitude": -102.31859 }, { "latitude": 31.85462, "longitude": -102.3165 }, { "latitude": 31.85467, "longitude": -102.31644 }, { "latitude": 31.85489, "longitude": -102.3162 }, { "latitude": 31.85505, "longitude": -102.3164 }, { "latitude": 31.8552, "longitude": -102.3166 }, { "latitude": 31.85533, "longitude": -102.31677 }, { "latitude": 31.85506, "longitude": -102.31706 }, { "latitude": 31.85655, "longitude": -102.32234 }, { "latitude": 31.85851, "longitude": -102.32294 } ], "summary": { "arrivalTime": "2020-06-04T01:22:22-05:00", "departureTime": "2020-06-03T17:28:11-05:00", "lengthInMeters": 863989, "trafficDelayInSeconds": 528, "travelTimeInSeconds": 28451 } } ], "sections": [ { "endPointIndex": 5797, "sectionType": "TRAVEL_MODE", "startPointIndex": 0, "travelMode": "car" } ], "summary": { "arrivalTime": "2020-06-04T01:22:22-05:00", "departureTime": "2020-06-03T17:28:11-05:00", "lengthInMeters": 863989, "trafficDelayInSeconds": 528, "travelTimeInSeconds": 28451 } } ]') t)
     ,flat_tbl as (select unnested.value json from tbl, lateral flatten(list_json) unnested)

select
    legs.value:summary.trafficDelayInSeconds trafficDelayInSeconds,
    legs.value:summary summary,
    points.value point
from flat_tbl,
    lateral flatten(json:legs) legs,
    lateral flatten(legs.value:points) points
;

它会产生这样的结果,这只是您在解析方面可能要做的猜测。

TRAFFICDELAYINSECONDS   SUMMARY POINT
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 29.76524,    "longitude": -95.35406  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74438,    "longitude": -101.71203  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74421,    "longitude": -101.71247  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74404,    "longitude": -101.71289  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74366,    "longitude": -101.71374  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74346,    "longitude": -101.71415  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74303,    "longitude": -101.71495  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74274,    "longitude": -101.71543  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 30.74234,    "longitude": -101.71606  }
528 {    "arrivalTime": "2020-06-04T01:22:22-05:00",    "departureTime": "2020-06-03T17:28:11-05:00",    "lengthInMeters": 863989,    "trafficDelayInSeconds": 528,    "travelTimeInSeconds": 28451  }  {    "latitude": 31.82985,    "longitude": -102.34753  }