最近在工做中遇到一個很難解析的JSON,他是一個嵌套的JSON數組的JSON,要使用Hive來進行解析,用Presto寫了一次,邏輯就很清晰,由於Presto自帶了JSON數據類型,轉換數組就很方便,而Hive解析完JSON數組後是一個字符串,只能使用split
方法來對string
類型的數據進行切分,因此若是遇到多層嵌套的數組,要注意切分方法,否則就會亂套。sql
{ "base": { "code": "xm", "name": "project" }, "list": [{ "ACode": "cp1", "AName": "Product1", "BList": [{ "BCode": "gn1", "BName": "Feature1" }, { "BCode": "gn2", "BName": "Feature2" }] }, { "ACode": "cp2", "AName": "Product2", "BList": [{ "BCode": "gn1", "BName": "Feature1" }] }] }
解析出來的結果應該以下表所示json
code | name | ACode | Aname | Bcode | Bname |
---|---|---|---|---|---|
xm | project | cp1 | Product1 | gn1 | Feature1 |
xm | project | cp1 | Product1 | gn2 | Feature2 |
xm | project | cp2 | Product2 | gn1 | Feature1 |
首先使用get_json_object
方法,把須要解析的數組解析出來,而後使用regexp_replace
將}]},{
替換成}]}||{
,而後再使用split
方法對||
進行分割,分割成數組後,使用lateral view explode
方法對其進行展開成多列即刻。數組
SELECT code , name , ai.ACode , ai.AName , bi.BCode , bi.BName FROM ( SELECT get_json_object(t.value, '$.base.code') AS code , get_json_object(t.value, '$.base.name') AS name , get_json_object(t.value, '$.list') AS list FROM ( SELECT '{"base":{"code":"xm","name":"project"},"list":[{"ACode":"cp1","AName":"Product1","BList":[{"BCode":"gn1","BName":"Feature1"},{"BCode":"gn2","BName":"Feature2"}]},{"ACode":"cp2","AName":"Product2","BList":[{"BCode":"gn1","BName":"Feature1"}]}]}' as value ) t ) t lateral view explode(split(regexp_replace(regexp_extract(list,'^\\[(.+)\\]$',1),'\\}\\]\\}\\,\\{', '\\}\\]\\}\\|\\|\\{'),'\\|\\|')) list as a lateral view json_tuple(a,'ACode','AName','BList') ai as ACode , AName , BList lateral view explode(split(regexp_replace(regexp_extract(BList,'^\\[(.+)\\]$',1),'\\}\\,\\{', '\\}\\|\\|\\{'),'\\|\\|')) BList as b lateral view json_tuple(b,'BCode','BName') bi as BCode , BName ;
執行完rest
xm project cp1 Product1 gn1 Feature1 xm project cp1 Product1 gn2 Feature2 xm project cp2 Product2 gn1 Feature1 Time taken: 0.787 seconds, Fetched: 3 row(s)
lateral view posexplode
方案,逐層解析,但這樣會致使笛卡爾。因此必須一次性所有解析好,而不是套用多個子查詢逐層解析;OUTER
字段,能使LATERAL VIEW
不忽略NULL
include
OUTER
in the query to get rows with NULL valuescodesomething like,regexp
select * FROM table LATERAL VIEW OUTER explode ( split ( email ,',' ) ) email AS email_id;