主要有3種方法:
前兩種內(nèi)置方法稚机,第三種是UDF幕帆,利用python腳本解析
- 1、get_json_object
- 2赖条、json_tuple
- 3失乾、python transform
具體的:
- 1、
get_json_object()
接受兩個參數(shù)纬乍,第一個參數(shù)是json對象變量碱茁;第二個參數(shù)用$
來表示json對象變量,用.和[]
來讀取對象或數(shù)組仿贬;
eg1:
select get_json_object('{"shop":{"book":[{"price":43.3,"type":"art"},{"price":30,"type":"technology"}],"clothes":{"price":19.951,"type":"shirt"}},"name":"jane","age":"23"}', '$.shop.book[0].type')
執(zhí)行結果:
art
如果json比較簡單:
eg2:
select get_json_object('{"name":"jack","server":"www.qq.com"}','$.server')
執(zhí)行結果:
www.qq.com
但是存在一個問題纽竣,一個get_json_object只能解析一個字段,如果要解析多個字段茧泪,則需要使用多個get_json_object來讀取退个,比較麻煩
- 2、json_tuple
可以同時解析一個json中的多個字段
eg:
select json_tuple('{"name":"jack","server":"www.qq.com"}','server','name')
執(zhí)行結果:
www.qq.com jack
但是存在的問題是调炬,只能解析簡單的json语盈,不能使用.和[]
的符號解析復雜json格式
- 3、python腳本解析(hive表中)
add file file_name;
insert overwrite table to_table
select
transform(col1,col2,col3)// from_table
using 'python file_name'
as
(uid,mid)//to_table
from from_table
python腳本中使用
import json
map = json.loads(line.strip())
uid = map.get('uid')