1

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.