hiveapache-hive

from string to map object in Hive


My input is a string that can contain any characters from A to Z (no duplicates, so maximum 26 characters it may have).

For example:-

set Input='ATK';

The characters within the string can appear in any order.

Now I want to create a map object out of this which will have fixed keys from A to Z. The value for a key is 1 if its corresponding character appears in the input string. So in case of this example (ATK) the map object should look like:-

Map object

So what is the best way to do this?

So the code should look like:-

set Input='ATK';
select <some logic>;

It should return a map object (Map<string,int>) with 26 key value pairs within it. What is the best way to do it, without creating any user defined functions in Hive. I know there is a function str_to_map that easily comes to mind.But it only works if key value pairs exist in source string and also it will only consider the key value pairs specified in the input.


Solution

  • Maybe not efficient but works:

    select str_to_map(concat_ws('&',collect_list(concat_ws(":",a.dict,case when 
    b.character is null then '0' else '1' end))),'&',':')
    from 
    (
       select explode(split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z",',')) as dict
    ) a 
    left join
    (
       select explode(split(${hiveconf:Input},'')) as character
    ) b 
    on a.dict = b.character
    

    The result:

    {"A":"1","B":"0","C":"0","D":"0","E":"0","F":"0","G":"0","H":"0","I":"0","J":"0","K":"1","L":"0","M":"0","N":"0","O":"0","P":"0","Q":"0","R":"0","S":"0","T":"1","U":"0","V":"0","W":"0","X":"0","Y":"0","Z":"0"}