HIVE中get_json_object與json_tuple使用及區別

場景:獲取hive表字段格式爲json的內部key信息

如下一條表記錄,表名:test,表字段:id,event_name,info,time.其中event_name爲string類型的json串

1588914335352   coupon_view    {"extend":{"pos":1,"cate_id":0,"cate_title":"推薦"},"object_info":{"fid":"38b7ivhA19gmbvMvIvFvsvqIvUvsv2b","coupon_promo_type":0,"item_id":615801170688,"mid":"","cate_id":7,"source_type":0,"stid":"53538217571492364","ticket_id":211110533,"subcate_id":22460,"product_type":1,"coupon_id":214699324,"rec_type":27,"final_price":"10.8","pos":28,"cate_id4":0,"cate_id3":22461,"is_improve":0,"platform_id":2,"origin_price":"15.8","zk_price":"10.8","raw_price":"15.8"},"base_info":{"from_spid":"60022.4|0.0","cur_module":"goodlist","from_page":"h5_orchardtask","from_module":"orchardtask_go","cur_page":"h5_orchard_browse","spid":"0.0"}}    96cd17c7-e2e0-4c3d-9cc8-0699465f8b9a    20200508

若我們想獲取info中的extend信息,則可以使用get_json_object和json_tuple,具體如下:

get_json_object:

select id,get_json_object(info,'$.extend') as extend from test limit 1;

json_tuple:

select id, extend from test lateral view json_tuple(info,'extend') tup as extend;

其中,需要使用lateral view 視圖方法來寫,不需要加$標示符讀取對象;

區別:json_tuple可以一次讀取多個字段,如下:

select id, extend, object_info from test lateral view json_tuple(info,'extend','object_info') tup as extend, object_info;

也可以組合使用:

select * from test
   lateral view json_tuple(info,'base_info','object_info','extend') tup2 as base_info,object_info,extend
   lateral view json_tuple(get_json_object(info,'$.base_info'),'cur_module','cur_page','from_module','from_page') tup2 as cur_module,cur_page,from_module,from_page
   lateral view json_tuple(get_json_object(info,'$.object_info'),'coupon_id') tup2 as coupon_id
   lateral view json_tuple(get_json_object(info, '$.extend'), 'cate_id') tup3 as cate_id;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章