hive 中 json 字符串解析之 get_json_object 与 json_tuple

在技术对app进行埋点时,会讲多个字段存放在一个数组中,因此模型调用数据时,要对埋点数据进行解析,以作进一步的清洗。本文将介绍解析json字符串的两个函数:get_json_object和json_tuple。

表结构如下:

其中meta 字段数据, 数据表是 test_table

{{"a":1,"b":2},{"a":3,"b":4}}

get_json_object

函数的作用:用来解析json字符串的一个字段:

select get_json_object(meta,'$.a') as filtertype
,get_json_object(meta,'$.b')as filtersubtype 
from  test_table

运行结果 仅有一条数据,其实应该是2条:

filtertype  filtersubtype
1					2

json_tuple
函数的作用:用来解析json字符串中的多个字段

select b.a 
,b.b
from test_table a
lateral view json_tuple(meta,'a', 'b', ) b as  
a, b; 

运行结果:

filtertype  filtersubtype
1					2
3					4

使用正则表达式对json 数据进行处理

对于上面的test_table 使用:

select get_json_object(B.stock_code,'$.a') as a,
get_json_object(B.stock_code,'$.b') as b,
from (
	select split(regexp_replace(regexp_extract(meta
	,'^\[(.+)\]$',1),'\\}\\,\\{', \'\}\\|\\|\\{''),'\\|\|') as stock_codes
	from test_table
) A lateral view explode(A.stock_codes) B as stock_code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝鲸123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值