要確保被解析的字段是string類型才可以使用json解析.解析map類型不能使用json解析,解析map類型可以使用col_name['key']獲取對應key的value.
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
2
3
4
5
6
7
8
9
10
1.get單層值
hive> select get_json_object(data, '$.owner') from test;
結果:amy
1
2
2.get多層值.
hive> select get_json_object(data, '$.store.bicycle.price') from test;
結果:19.95
1
2
3.get數組值[]
hive> select get_json_object(data, '$.store.fruit[0]') from test;
結果:{"weight":8,"type":"apple"}