当前位置:   article > 正文

Hive解析多重嵌套JSON数组_hive 解析数组嵌套

hive 解析数组嵌套

嵌套的JSON数组的JSON,要使用Hive来进行解析

需要解析的JSON

{
	"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"
		}]
	}]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

解析出来的结果应该如下表所示

codenameACodeAnameBcodeBname
xmprojectcp1Product1gn1Feature1
xmprojectcp1Product1gn2Feature2
xmprojectcp2Product2gn1Feature1

解决方案

首先使用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
;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

执行完成

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)
  • 1
  • 2
  • 3
  • 4
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号