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;

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值