赞
踩
select
get_json_object('{"user_name":"chimchim","age":30}', '$.user_name') as user_name,
get_json_object('{"user_name":"chimchim","age":30}', '$.age') as age
select json_tuple('{"user_name":"chimchim","age":30,"sex":"woman"}', 'user_name', 'age','sex')
select json_tuple(json, 'user_name', 'age', 'sex')
from (
select explode( --将json数组中的元素解析出来,转化为每行显示
split(regexp_replace(regexp_replace(
'[{"user_name":"chimchim","age":30,"sex":"woman"},{"user_name":"zonzon","age":2,"sex":"man"}]' --要解析的json内容
, '\\[|\\]', '') --将json数组两边的中括号去掉
,'\\}\\,\\{', '\\}\\;\\{') --将json数组元素之间的逗号换成分号
, '\\;')) --以分号作为分隔符(split函数以分号作为分隔)
as json) o;
explode函数
select array('A','B','C') ;
regexp_replace函数
--将,替换为;
select regexp_replace('{"user_name":"chimchim","age":30,"sex":"woman"}', ',', ';');
lateral view
说明:lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据,在此基础上可以对拆分的数据进行聚合,lateral view首先为原始表的每行调用UDTF,UDTF会把一行拆分成一行或者多行,lateral view在把结果组合,产生一个支持别名表的虚拟表。
原始数据
select 'chimchim' as user_name,array("a","b","c") as class;
解析后
select user_name,class_str
from (select 'chimchim' as user_name,array("a","b","c") as class) a
lateral view explode(class) tmp_table as class_str;
使用 lateral view 解析json数组
--第一种写法
select
get_json_object(tmp,'$.user_name') as user_name
,get_json_object(tmp,'$.age') as age
,get_json_object(tmp,'$.sex') as sex
from (select '[{"user_name":"chimchim","age":30,"sex":"woman"},{"user_name":"zonzon","age":2,"sex":"man"}]' as json_str) a
lateral view explode(split(regexp_replace(regexp_replace(json_str , '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;')) tmp as tmp;
--第二种写法
select user_name,age,sex
from (
select '[{"user_name":"chimchim","age":30,"sex":"woman"},{"user_name":"zonzon","age":2,"sex":"man"}]' as json
) t
lateral view explode(split(regexp_replace(regexp_replace(regexp_replace(json, '\\[|\\]',''),'\\s',''),'\\}\\,\\{','\\}\\;\\{'),'\\;')) tmp1 as regexp_json
lateral view json_tuple(regexp_json,'user_name','age','sex') tmp2 as user_name,age,sex
;
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
9ld-4701985574679)]
[外链图片转存中…(img-k14DhEHP-4701985574679)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。