Как конвертировать JSONARRAY в мульти столбец из улья - PullRequest
1 голос
/ 03 апреля 2020

пример: существует столбец массива json (тип: строка) из таблицы улья, например:

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

как преобразовать его в:

name      age
alice     14

с помощью улья sql? Я пробовал боковой вид взорваться, но он не работает. Большое спасибо!

1 Ответ

1 голос
/ 03 апреля 2020

Это рабочий пример того, как его можно проанализировать в 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

Еще один подход с использованием get_json_object:

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 = 'name' then value end) as name,
       max(case when field = 'age'  then value end) as age        --do the same for all fields 
from
(
select t.id,
       get_json_object(trim(a.field),'$.field') field,
       get_json_object(trim(a.field),'$.value') value
  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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...