2

I have a hive table like

name            string                                      
address         string                                      
timezone        string                                      
one_key_value   array<struct<key:string,value:array<string>>                    
two_key_value   array<struct<key:string,value:array<string>>

and want to convert it to

name            string                                      
address         string                                      
timezone        string                                      
one_key_value   map<string,array<string>>                       
two_key_value   map<string,array<string>>

There is explode(array) but doesn't really return the entire table in the format I want.

1 Answer 1

3

Use lateral view with inline and map the resulting keys and values.

select name,address,timezone,map(k1,v1),map(k2,v2)
from tbl 
lateral view inline(one_key_value) t1 as k1,v1
lateral view inline(two_key_value) t1 as k2,v2
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to not specify column names and just do * as there are more than 50 columns.
you can do it in select, but note you will get the original and transformed columns in that case.
How can I add a where statement to this, like where date='2018-10-10'
you can use it at the end..after lateral view
I just realized this is exploding the table and creating a new row for each value. I just need a single row for a key and all it's value. Any idea what changes I should make to this? Thanks :)

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.