无法在配置单元中分解 json 字符串
Not able to explode json string in hive
[{"name":"john","id":12,"location":"delhi"},{"name":"raj","id":18,"location":"mumbai"},{"name":"Rahul","id":14,"location":"hyd"}]
在配置单元 table 中的一条记录上使用 get_json_object
后,我得到了这个字符串(不是结构)。我需要分解它以创建一个新的 table,其列名称为名称、ID 和位置。有人可以帮忙吗
Explode()
或 inline()
接受结构数组。已尝试 explode(Array(struct(my get_json_object output)))
但没有给出预期的结果。
首先将字符串转换为 JSON 个对象的数组:删除方括号,在大括号之间用逗号分隔并展开。然后使用 json_tuple 和横向视图来提取所有值。请参阅此演示:
with mytable as (--demo table, use your table instead
select '[{"name":"john","id":12,"location":"delhi"},{"name":"raj","id":18,"location":"mumbai"},{"name":"Rahul","id":14,"location":"hyd"}]' as json_string
)
select --t.json_string as original_string, --commented
e.pos as position_in_array,
--values from json
x.name, x.id, x.location
from mytable t
lateral view outer posexplode( split(regexp_replace(json_string,'^\[|\]$',''), --remove []
'(?<=\}),(?=\{)' --split by comma only after } and before {
) --converted to array of json strings
)e as pos, json --exploded array element with position
--extract all from e.json
lateral view json_tuple(e.json,'name', 'id', 'location') x as name, id, location
结果:
position_in_array x.name x.id x.location
0 john 12 delhi
1 raj 18 mumbai
2 Rahul 14 hyd
[{"name":"john","id":12,"location":"delhi"},{"name":"raj","id":18,"location":"mumbai"},{"name":"Rahul","id":14,"location":"hyd"}]
在配置单元 table 中的一条记录上使用 get_json_object
后,我得到了这个字符串(不是结构)。我需要分解它以创建一个新的 table,其列名称为名称、ID 和位置。有人可以帮忙吗
Explode()
或 inline()
接受结构数组。已尝试 explode(Array(struct(my get_json_object output)))
但没有给出预期的结果。
首先将字符串转换为 JSON 个对象的数组:删除方括号,在大括号之间用逗号分隔并展开。然后使用 json_tuple 和横向视图来提取所有值。请参阅此演示:
with mytable as (--demo table, use your table instead
select '[{"name":"john","id":12,"location":"delhi"},{"name":"raj","id":18,"location":"mumbai"},{"name":"Rahul","id":14,"location":"hyd"}]' as json_string
)
select --t.json_string as original_string, --commented
e.pos as position_in_array,
--values from json
x.name, x.id, x.location
from mytable t
lateral view outer posexplode( split(regexp_replace(json_string,'^\[|\]$',''), --remove []
'(?<=\}),(?=\{)' --split by comma only after } and before {
) --converted to array of json strings
)e as pos, json --exploded array element with position
--extract all from e.json
lateral view json_tuple(e.json,'name', 'id', 'location') x as name, id, location
结果:
position_in_array x.name x.id x.location
0 john 12 delhi
1 raj 18 mumbai
2 Rahul 14 hyd