先貼一下hive中get_json_object和json_tuple兩個函數(shù)的區(qū)別:
Hive中提供了兩種針對json數(shù)據(jù)格式解析的函數(shù)焊切,即get_json_object(…)與json_tuple(…)板鬓,理論不多說延曙,直接上效果示意圖:
假設存在如下json數(shù)據(jù)對象大诸,若使用hive環(huán)境可這么設置:
set hivevar:msg={
“message”:”2015/12/08 09:14:4”,
“client”: “10.108.24.253”,
“server”: “passport.suning.com”,
“request”: “POST /ids/needVerifyCode HTTP/1.1”,
“server”: “passport.sing.co”,
“version”:”1”,
“timestamp”:”2015-12-08T01:14:43.273Z”,
“type”:”B2C”,”center”:”JSZC”,
“system”:”WAF”,”clientip”:”192.168.61.4”,
“host”:”wafprdweb03”,
“path”:”/usr/local/logs/waf.error.log”,
“redis”:”192.168.24.46”}
get_json_object函數(shù)
當使用get_json_object函數(shù)時,則可以這么用:
select get_json_object(‘hivevar:msg′,′hivevar:msg′,′.server’) from test;
返回:
passport.sing.com
其中,get_json_object函數(shù)第一個參數(shù)填寫json對象變量,第二個參數(shù)使用$表示json變量標識郑藏,然后用 . 或 [] 讀取對象或數(shù)組;
json_tuple函數(shù)
當使用json_tuple對象時瘩欺,可以顯著提高效率必盖,一次獲取多個對象并且可以被組合使用,寫法如下:
select a.* from test lateral view json_tuple(‘${hivevar:msg}’,’server’,’host’) a as f1,f2;
返回:
passport.sing.com wafprdweb03
其中俱饿,需要使用lateral view 視圖方法來寫歌粥,不需要加$標示符讀取對象,獲取的f1,f2對象可用于array()或map()等函數(shù)使用
以下是正文:
我有一個表tab1拍埠,表有兩個字段sub_id, sub_sku_info阁吝,表中有一條數(shù)據(jù)如下:
(注:第二個字段比較長,但這確實是一個字符串械拍,請不要被換行欺騙):
sub_id : 101
sub_sku_info :
[{"skuId":"1017570","num":"2","price":5.8,"jd_price":23.9,"sale_price":5.8},{"skuId":"1329431","num":"1","price":38.38,"jd_price":59,"sale_price":36.84},{"skuId":"1381473","num":"1","price":8.5,"jd_price":39.8,"sale_price":8.5}]
讓我們逐步來拆解這條數(shù)據(jù):
1突勇、將sub_sku_info中的“列表”拆成三行:
select sub_id, sku_infofrom tab1
lateral view explode(split(substr(sub_sku_info, 2, length(sub_sku_info) - 2), '},')) aa as sku_info;
2、補全每一行記錄坷虑,為第三步拆分做準備:
select t.sub_id, case when substr(t.sku_info, length(t.sku_info), 1) = '}' then t.sku_info else concat(t.sku_info, '}') end as sku_infofrom (select sub_id, sku_infofrom tab1
lateral view explode(split(substr(sub_sku_info, 2, length(sub_sku_info) - 2), '},')) aa as sku_info) t;
3甲馋、將每一行的“字典”拆分成對應字段:
select m.sub_id, skuid, num, price, jd_price, sale_price
from(
select t.sub_id, case when substr(t.sku_info, length(t.sku_info), 1) = '}' then t.sku_info else concat(t.sku_info, '}') end as sku_info
from (
select sub_id, sku_info
from tab1
lateral view explode(split(substr(sub_sku_info, 2, length(sub_sku_info) - 2), '},')) aa as sku_info) t) m
lateral view json_tuple(m.sku_info, 'skuId', 'num', 'price', 'jd_price', 'sale_price') aa as skuid, num, price, jd_price, sale_price;