Presto:从字符串列中提取值,类似于map,但不是map
Presto: Extract values from a string column, which is similar to map, but not a map
我有一个table:
Name pets
--------------
Andy {"cat":2, "dog":1, "bird":4 ,xxx}
John {"dog":3, "cat":1, "bird":{}, uyx}
Mary {"dog":2, "duck":{}, "cat":1, zzz}
宠物栏是一张地图,但 table 创建者制作的是一个字符串,其中有一些额外的字符。所以我不能使用 cast(json_parse(pets) as map(varchar, varchar)) AS m
.
在这种情况下,如果我想找出"cat"
的值,我该怎么做呢?谢谢!
您可以使用 regexp_extract()
:
select t.*,
regexp_extract(pets, '"cats":([^,]*)', 1)
from t;
我有一个table:
Name pets
--------------
Andy {"cat":2, "dog":1, "bird":4 ,xxx}
John {"dog":3, "cat":1, "bird":{}, uyx}
Mary {"dog":2, "duck":{}, "cat":1, zzz}
宠物栏是一张地图,但 table 创建者制作的是一个字符串,其中有一些额外的字符。所以我不能使用 cast(json_parse(pets) as map(varchar, varchar)) AS m
.
在这种情况下,如果我想找出"cat"
的值,我该怎么做呢?谢谢!
您可以使用 regexp_extract()
:
select t.*,
regexp_extract(pets, '"cats":([^,]*)', 1)
from t;