husna anjum
husna anjum

Reputation: 19

Convert Map<string,string> to just string in hive

Anyone please help me with this, how do we convert the map<string,string> type to string data type in hive?

Upvotes: 1

Views: 7703

Answers (1)

leftjoin
leftjoin

Reputation: 38325

Explode map then concatenate key with value, collect all the key:value pairs into array and concatenate array using concat_ws.

Demo:

with test_data as (
select stack(2,
             1, map('key1', 'val1', 'key2', 'val2'),
             2, map('key1', 'val1', 'key2', 'val2')
            ) as (id, map_col)
)

select id, map_col as original_map,
       concat_ws(',',collect_set(concat(m.key,':', m.val))) map_str
  from test_data d
       lateral view explode(map_col) m as key, val
 group by id, map_col

Result:

id   original_map                   map_str
1   {"key1":"val1","key2":"val2"}   key1:val1,key2:val2
2   {"key1":"val1","key2":"val2"}   key1:val1,key2:val2

You can use some other delimiters when concatenating.

Upvotes: 5

Related Questions