场景:获取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;

 

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐