目录

一、背景

二、hive 解析 json 数据函数

1、get_json_object 

2、json_tuple

3、使用嵌套子查询(explode+regexp_replace+split+json_tuple)解析json数组

4、使用 lateral view 解析json数组

5、解析非固定名称json

一、背景

我们进行ETL(Extract-Transfer-Load)  过程中,经常会遇到从不同数据源获取的不同格式的数据,其中某些字段就是json格式,里面拼接了很多字段key和指标值value,今天讲一下如何解析出来相关数据。

二、hive 解析 json 数据函数

1、get_json_object 

语法:get_json_object(json_string, '$.key')说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。这个函数每次只能返回一个数据项。优势:一次可以解析一个json字段

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

2、json_tuple

语法: json_tuple(json_string, k1, k2 ...)说明:解析json的字符串json_string,可指定多个json数据中的key,返回对应的value。如果输入的json字符串无效,那么返回NULL。优势:一次可以解析多个json字段

select json_tuple('{"user_name":"chimchim","age":30,"sex":"woman"}', 'user_name', 'age','sex')

3、使用嵌套子查询(explode+regexp_replace+split+json_tuple)解析json数组

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函数

语法:explode(Array OR Map)说明:explode()函数接收一个array或者map类型的数据作为输入,然后将array或map里面的元素按照每行的形式输出,即将hive一列中复杂的array或者map结构拆分成多行显示,也被称为列转行函数。

select array('A','B','C') ;

 

regexp_replace函数

语法: regexp_replace(string A, string B, string C)说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符,类似oracle中的regexp_replace函数。

--将,替换为;

select regexp_replace('{"user_name":"chimchim","age":30,"sex":"woman"}', ',', ';');

 

4、使用 lateral view 解析json数组

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

;

5、解析非固定名称json

select

json

,regrep_json

,split(regrep_json,':')[0] as key1

,split(regrep_json,':')[1] as value1

from (

select '{"a":0.1,"b":0.2,"c":0.3,"d":0.4,"e":0.5,"f":0.6,"g":0.7}' as json

) t

lateral view explode(split(regexp_replace(regexp_replace(json, '\\{|\\}|\\"',''),'\\s',''),'\\,')) tmp1 as regrep_json

;

 

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。