如何在Hive中将字符串“ABCD”转换为字符串“A:1,B:1,C:1,D:1”?一旦我以“A:1,B:1,C:1,D:1”的形式得到“ABCD”,我的目标是使用str_to_map函数将其转换为map。该数字固定为1,因此不会有任何其他数字。但字符串的长度可能会有所不同,任何字母都可能出现在那里。没有重复。另一个例子是: -
Input string is "PKWQADT"
Desired output string is "P:1,K:1,W:1,Q:1,A:1,D:1,T:1"
我的最终目标是在修改后的字符串上调用此函数: -
str_to_map(modifiedstring)
答案 0 :(得分:1)
可能效率不高但有效:
select str_to_map(concat(concat_ws(':1,',collect_set(a_col)),':1')) as a_map --collect_set returns array then concat string from array using ':1,' as a delimiter
from
(select split('PKWQADT','') as a_array --split array using '' as delimiter
from dual
)s lateral view outer explode(a_array) ar as a_col
where ar.a_col<>'' and ar.a_col is not null --filter out first and last empty elements
;
OK
{"D":"1","T":"1","W":"1","Q":"1","P":"1","A":"1","K":"1"}
Time taken: 48.246 seconds, Fetched: 1 row(s)
答案 1 :(得分:0)
这是我能得到的最接近的: -
hive> select concat_ws(":1,",split("ABC",""));
OK
:1,A:1,B:1,C:1,
然后在它上面应用str_to_map: -
hive> select str_to_map(concat_ws(":1,",split("ABC","")));
OK
{"":"1","C":"1","A":"1","B":"1"}
它几乎看起来很理想。如果我能摆脱空白钥匙那就太好了。