參考 https://blog.csdn.net/qq_34105362/article/details/80454697
hive提供了json的解析函數:get_json_object(string json_string, string path)
第一個參數填寫json對象變量,第二個參數使用$表示json變量標識,然后用 . 或 [] 讀取對象或數組;如果輸入的json字符串無效,那么返回NULL。
每次只能返回一個數據項。
舉例:
data 為 test表中的字段,數據結構如下:
data = { "store": { "fruit":[{"weight":8,"type":"apple"}, {"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} }, "email":"amy@only_for_json_udf_test.net", "owner":"amy" }
1.get單層值
hive> select get_json_object(data, '$.owner') from test; 結果:amy
2.get多層值.
hive> select get_json_object(data, '$.store.bicycle.price') from test; 結果:19.95
3.get數組值[]
hive> select get_json_object(data, '$.store.fruit[0]') from test; 結果:{"weight":8,"type":"apple"}