Улей извлекать ключи из строки типа dict с помощью regexp_extract? - PullRequest
0 голосов
/ 26 января 2019

Я хочу извлечь ключи из столбца из таблицы улья, которому это нравится ниже

{"agya":3,"gentong":1,"tronton":0,"tasikmalaya":4,"tanja":2}
{"afifah":3,"sctv":10,"samuel zylgwyn":2,"naysila mirdad":0,"shared":8}
{"aferia":1,"jatimtimes":3,"apbdes":2,"siltap":4,"mudjito":0}
{"aerox":0,"flasher":1,"lampu hazard":2,"aftermarket":4,"dcs":5}
{"administratif":6,"fakta":7,"prabowo":5,"cek":4,"admistratif":0}
{"adeg":2,"tiru":1,"film film":3,"romantis":0,"nggak":5}

для первого, который я хочу получить "agya", "gentong", "tronton" и т. Д. А позже я могу разбить их на несколько строк,Как использовать regexp_extract для реализации этого?

Ответы [ 2 ]

0 голосов
/ 30 января 2019

Вы можете попробовать следующее решение:

select map_keys(str_to_map(regexp_replace(mycol,'[{}"]','')));

Здесь

1.regexp_replace function is used to replace all the '{','}','"' characters with nothing.
2.str_to_map function has beeen used to convert the string to map.
3.map_keys function is used to extract the keys from the map which will give the result in an array format.
4.You can then explode this array as per your need.

Спасибо

0 голосов
/ 26 января 2019

regexp_extract() возвращает строку. Чтобы получить массив, используйте функцию split(), в качестве шаблона-разделителя также используется регулярное выражение. Таким образом, вы можете разделить на ':\\d+,'

split(
     regexp_replace(col, '^\\{|\\}$',''), --remove outer curly braces {}
     ':\\d+,' --array elements delimiter pattern
     ) --this will give array "agya", "gentong", etc

После взрыва массива вы можете удалить кавычки, используя regexp_replace(col_exploded,'\\"','')

Обновление

Последний ключ: значение не содержит ,, поэтому необходимо исправить шаблон и использовать ,|$ (запятая или конец строки). Также последний элемент будет пустым, его нужно отфильтровать.

Тест:

hive> select regexp_replace(key,'\\"','') key
    > from
    > (
    > select explode(
    > split(
    >      regexp_replace('{"agya":3,"gentong":1,"tronton":0,"tasikmalaya":4,"tanja":2}', '^\\{|\\}$',''), --remove outer curly braces {}
    >      ':\\d+(,|$)' --array elements delimiter pattern
    >      )
    > ) as key
    > )s
    > where key!=''
    > ;
OK
agya
gentong
tronton
tasikmalaya
tanja
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...