hive解析json中jsonArray

json如下

{"cityId":1000,"content":{"handleCause":2,"violationListId":"3b643fc1-af48-11ec-bc68-2c4d54472e87,3b62be33-af48-11ec-bc68-2c4d54472e87,3b61d6eb-af48-11ec-bc68-2c4d54472e87","handleType":3,"phone":"175****9875","dbTime":"2022-03-29 18:30:00","startTime":"2022-03-30 00:30:00","endTime":"2022-04-29 00:30:00","blackListBehaviorList":[{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b5cb3dd-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b5d9b7b-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b5e1780-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b5f8762-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b61d6eb-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b62be33-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b643fc1-af48-11ec-bc68-2c4d54472e87","dataSources":3},{"phone":"175****9875","dbTime":"2022-03-29 18:07:58","id":"3b654a62-af48-11ec-bc68-2c4d54472e87","dataSources":3}]},"logFormat":"json","logName":"数据","logType":600021}

解析json中的jsonArray得出具体字段对应的值

select 
	get_json_object(a_json,'$.phone') as phone, -- 解析单个json
	get_json_object(a_json,'$.dbTime') as dbTime,
	get_json_object(a_json,'$.dataSources') as dataSources
from
(
select split(regexp_replace(regexp_extract(  get_json_object(get_json_object(dataobj,"$.content"),"$.blackListBehaviorList") ,'(\\[)(.*?)(\\])',2),'\\},\\{','\\}|\\{'),'\\|') as a_list -- 从json中解析出jsonarray在进行替换掉json的字符形成单个json  其中dataobj是表中的字段名称
from **库名.表名**
       where
            concat_ws('-', year, month, day) = '2022-05-24'
) a
lateral view explode(a_list) a_list_tab as a_json
;

解析结果
hive解析json中jsonArray_第1张图片
– 关联解析出具体的json对应的jsonArray数据形成一条数据

select * from(
    select
        case when get_json_object(get_json_object(dataobj, "$.content"),"$.handleCause")=1 then '违法'
            else '违规'
        end  as handleCause,
        case when get_json_object(get_json_object(dataobj, "$.content"),"$.handleType")=1 then '提醒' 
            when get_json_object(get_json_object(dataobj, "$.content"),"$.handleType")=2 then '警告'
            else '禁骑'
        end as handleType,
        get_json_object(get_json_object(dataobj, "$.content"),"$.phone") as phone,
        get_json_object(get_json_object(dataobj, "$.content"),"$.dbTime") as dbTime,
        get_json_object(get_json_object(dataobj, "$.content"),"$.startTime") as startTime,
        get_json_object(get_json_object(dataobj, "$.content"),"$.endTime") as endTime
        
    from
        hm_tog.ods_log_htw_policy_gateway_bigdata_log_d
    where
        concat_ws('-', year, month, day) = '2022-05-24'
)a 
left join (
    select 
        get_json_object(a_json,'$.phone') as phone,
        get_json_object(a_json,'$.dbTime') as dbTime,
        case when get_json_object(a_json,'$.dataSources')=1 then '交警处罚' 
             when get_json_object(a_json,'$.dataSources')=2 then '平台监测'
             else '企业上报'
        end as dataSources
    from
    (
    select split(regexp_replace(regexp_extract( 
        get_json_object(get_json_object(dataobj,"$.content"),"$.blackListBehaviorList")
        ,'(\\[)(.*?)(\\])',2),'\\},\\{','\\}|\\{'),'\\|') as a_list
    from hm_tog.ods_log_htw_policy_gateway_bigdata_log_d 
        where
                concat_ws('-', year, month, day) = '2022-05-24'
    ) a
    lateral view explode(a_list) a_list_tab as a_json
    
) b on a.phone=b.phone
  
;

解析结果
hive解析json中jsonArray_第2张图片

你可能感兴趣的:(hive,json,hive,restful)