简体   繁体   中英

str_to_hive function splits each character of the string as key

I have a string "description=Qiemo|locationToolsID=733" in one of the string column in hive. When I try to convert this to map using the delimiter | for each KV and = for key and value, it doesn't work.

Specifically, when I run:

str_to_map(str('description=Qiemo|locationToolsID=733'),'|','=').

I was expecting

description:Qiemo
locationToolsID:733

but I am getting like this

{"a":null,"":"","c":null,"d":null,"D":null,"e":null,"i":null,"I":null,"l":null,"m":null,"n":null,"o":null,"p":null,"Q":null,"r":null,"s":null,"3":null,"t":null,"T":null,"7":null,"|":null}

What is going on here?

You have to escape the | character as it has special meaning in Java regular expression. You can try this:

select str_to_map("description=Qiemo|locationToolsID=733",'\\\|','=');
                         OR
select str_to_map("description=Qiemo|locationToolsID=733",'[|]','=');

Output would be: {"locationToolsID":"733","description":"Qiemo"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM