json_tuple,get_json_object提取日志埋点信息
生活随笔
收集整理的這篇文章主要介紹了
json_tuple,get_json_object提取日志埋点信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
現數據庫有一張日志表,有個字段存放的json字符串,需要獲取orders數組里的is_store_delivery,delivery_warehouse字段
以下是該字段的json數據
{"returnCode":"0","result":{"total":2,"orders":[{"buyer_province":"廣東省","transport_time":"","buyer_country_id":"CN","remark":"","transport_no":"05365368605","merged_sn":"","order_status":"23","delivery_kpi_start_time":"2021-09-06 22:31","buyer_mobile":"138****3907","order_goods":[{"vendor_code":"10**06","quantity":1,"size":"均碼","price":"147.00","vendor_id":22,"brand_name":"XXXXX","sn":"KYT**-15AW","cooperation_no":"300921","barcode":"6939426","product_name":"迷你電風扇臺扇","promotion_price":"","po_no":"21030877"}],"pay_type":"移動微信wap支付","package_received_time":"2021-09-07","transport_day":"送貨時間不限","parent_order_sn":"","cod_type":"0","is_forbidden_delivery":0,"is_pre_transport_no":"0","carrier_name":"速運","order_type":1,"buyer_county":"海珠區","cod_money":"0.00","delivery_warehouse":"SPD589","buyer_address":"廣東省.廣州市.海珠區","buyer_postcode":"5120","vendor_name":"深圳市有限公司","buyer_city":"廣州市","buyer":"楊***","merged_code":"","last_update_time":"2021-09-08 10:10:02.0","buyer_tel":"","is_store_delivery":0,"vendor_id":22,"add_time":"2021-09-06 21:58:59.0","order_sn":"21092226","carrier_code":"shunfeng"},{"buyer_province":"廣東省","transport_time":"","buyer_country_id":"CN","remark":"","transport_no":"05378847236","merged_sn":"","order_status":"23","delivery_kpi_start_time":"2021-09-07","buyer_mobile":"137****6112","order_goods":[{"vendor_code":"1006","quantity":1,"size":"均碼","price":"188.00","vendor_id":22,"brand_name":"XXXXXXXXX","sn":"FSYF","cooperation_no":"3001","barcode":"6934680","product_name":"低噪搖頭風扇落地扇","promotion_price":"","po_no":"2103077"}],"pay_type":"微信支付app","package_received_time":"2021-09-07","transport_day":"送貨時間不限","parent_order_sn":"","cod_type":"0","is_forbidden_delivery":0,"is_pre_transport_no":"0","carrier_name":"順豐速運","order_type":1,"buyer_county":"端州區","cod_money":"0.00","delivery_warehouse":"SPD09","buyer_address":"廣東省.肇慶市.端州區","buyer_postcode":"520","vendor_name":"深圳市有限公司","buyer_city":"肇慶市","buyer":"黃*","merged_code":"","last_update_time":"2021-09-08","buyer_tel":"","is_store_delivery":0,"vendor_id":22,"add_time":"2021-09-07 12:48:28.0","order_sn":"21083502","carrier_code":"shunfeng"}]}}對應SQL,獲取數組里的值并將其轉換為列(行轉列)
select distinctdelivery_warehouse,is_store_delivery,vendor_id from(SELECT split(regexp_replace(regexp_extract(t.json_column, '^\\[(.+)\\]$',1),'\\}\\,\\{', '\\}\\|\\|\\{'),'\\|\\|') AS json_columnFROM -- 將response替換為上述字符串即可運行(select get_json_object('上述json字符串', '$.result.orders') as json_column ) t ) a lateral view explode(json_column) b as j_column LATERAL VIEW json_tuple(j_column, 'delivery_warehouse', 'is_store_delivery', 'vendor_id') d AS delivery_warehouse, is_store_delivery, vendor_id?運行效果圖:
?獲取json數據的思路:
-- (1)json_tuple()獲取json腳本 里面json節點orders
-- (2)1)regexp_extract()去掉中括號[]
-- 2)regexp_extract()用雙豎線替換 },{
? ? -- 3)split()根據雙豎線分割數組為多個小的json
-- (3)json_tuple()獲取json下vendor_id等節點值
附加一個presto的寫法:
select distinct vendor_id,x.delivery_warehouse,x.is_store_delivery from hive.xxx cross join unnest(cast(json_extract("json字符串", '$.result.orders') as array(row(delivery_warehouse VARCHAR,is_store_delivery INTEGER )))) as x總結
以上是生活随笔為你收集整理的json_tuple,get_json_object提取日志埋点信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 那些有趣的网站系列(十五)
- 下一篇: 【运维小工具】 - Mac OS 如何查