Hive复杂数组字典(Json-Array)解析

数据存储字段格式如下(Json-Array互相嵌套):
string='{"id":"9088848902695992720","title":"猛犸电动(共和新路旗舰店)","address":"上海市静安区共和新路492号","category":"购物:自行车专卖","type":0,"location":{"lat":31.251923,"lng":121.463251},"adcode":"310106","province":"上海市","city":"上海市","district":"静安区","fromSearch":1,"range":true,"name":"猛犸电动(共和新路旗舰店)","tag":false,"price":false,"description":"上海市静安区共和新路492号","area_id":"310106","business_name":"猛犸电动(共和新路旗舰店)","latitude":31.251923,"longitude":121.463251,"poi_name":"上海市 · 猛犸电动(共和新路旗舰店)","poi_id":"9088848902695992720","level":1,"selected":true,"rangeDistance":"2.0","citycode":"310000","extra_items":[{"id":"9088848902695992720","title":"猛犸电动(共和新路旗舰店)","address":"上海市静安区共和新路492号","category":"购物:自行车专卖","type":0,"location":{"lat":31.251923,"lng":121.463251},"adcode":"310106","province":"上海市","city":"上海市","district":"静安区","fromSearch":1,"range":true,"name":"猛犸电动(共和新路旗舰店)","tag":false,"price":false,"description":"上海市静安区共和新路492号","area_id":"310106","business_name":"猛犸电动(共和新路旗舰店)","latitude":31.251923,"longitude":121.463251,"poi_name":"上海市 · 猛犸电动(共和新路旗舰店)","poi_id":"9088848902695992720","level":1,"selected":true,"rangeDistance":"2.0","citycode":"310000"},{"id":"1737914136852040362","title":"猛犸电动(中山南二路旗舰店)","address":"上海市徐汇区中山南二路861号","category":"购物:自行车专卖","type":0,"location":{"lat":31.184747101,"lng":121.450555437},"adcode":"310104","province":"上海市","city":"上海市","district":"徐汇区","fromSearch":1,"range":true,"name":"猛犸电动(中山南二路旗舰店)","tag":false,"price":false,"description":"上海市徐汇区中山南二路861号","area_id":"310104","business_name":"猛犸电动(中山南二路旗舰店)","latitude":31.184747,"longitude":121.450555,"poi_name":"上海市 · 猛犸电动(中山南二路旗舰店)","poi_id":"1737914136852040362","level":1,"selected":true,"rangeDistance":"2.0","citycode":"310000"},{"id":"2079399087608466027","title":"猛犸电动专卖店(共江路店)","address":"上海市宝山区共江路204号","category":"购物:自行车专卖","type":0,"location":{"lat":31.331000146,"lng":121.456731092},"adcode":"310113","province":"上海市","city":"上海市","district":"宝山区","fromSearch":1,"range":true,"name":"猛犸电动专卖店(共江路店)","tag":false,"price":false,"description":"上海市宝山区共江路204号","area_id":"310113","business_name":"猛犸电动专卖店(共江路店)","latitude":31.331,"longitude":121.456731,"poi_name":"上海市 · 猛犸电动专卖店(共江路店)","poi_id":"2079399087608466027","level":1,"selected":true,"rangeDistance":"2.0","citycode":"310000"}]}'


select poi_name_now 
    ,get_json_object(m, '$.poi_name') as poi_name
from (
    select get_json_object(string, '$.poi_name') as poi_name_now --当前门店
        ,get_json_object(string, '$.extra_items') as extra_it --所有门店
    from table
)
LATERAL view explode(split(regexp_replace(extra_it,'^\\[|]$',''), ',(?!")')) ext_it as m
group by poi_name_now,get_json_object(m, '$.poi_name')

最终展示结果:

参考链接:https://stackoverflow.com/questions/62512180/hive-explode-each-json-element-in-json-array-to-rows

 

posted @ 2021-11-29 16:59  小碗吃不胖的  阅读(1566)  评论(0编辑  收藏  举报