如何将jsonarray从配置单元转换为多列

问题描述 投票:0回答:1

示例:蜂巢表中有一个json数组列(类型:字符串),例如:

"[{"filed":"name", "value":"alice"}, {"filed":"age", "value":"14"}......]"

如何将其转换为:

name      age
alice     14

通过配置单元sql?我尝试过横向爆炸,但无法正常工作。非常感谢!

sql json hive hiveql
1个回答
0
投票

这是如何在Hive中进行解析的有效示例。自行定制并调试实际数据,请参见代码中的注释:

with your_table as (
select stack(1,
1,
'[{"field":"name", "value":"alice"}, {"field":"age", "value":"14"}, {"field":"something_else", "value":"somevalue"}]'
) as (id,str) --one row table with id and string with json. Use your table instead of this example
)


select id, 
       max(case when field_map['field'] = 'name' then field_map['value'] end) as name,
       max(case when field_map['field'] = 'age'  then field_map['value'] end) as age        --do the same for all fields 
from
(
select t.id,
       t.str as original_string,
       str_to_map(regexp_replace(regexp_replace(trim(a.field),', +',','),'\\{|\\}|"','')) field_map --remove extra characters and convert to map
  from your_table t
       lateral view outer explode(split(regexp_replace(regexp_replace(str,'\\[|\\]',''),'\\},','}|'),'\\|')) a as field --remove [], replace "}," with '}|" and explode 
) s 
group by id --aggregate in single row
; 

结果:

OK
id      name    age
1       alice   14
© www.soinside.com 2019 - 2024. All rights reserved.