7

I have a table with 4 columns, one column (items) type is ARRAY and other are string.

ID   |    items                                  | name  |  loc  
_________________________________________________________________

id1  | ["item1","item2","item3","item4","item5"] | Mike | CT
id2  | ["item3","item7","item4","item9","item8"] | Chris| MN
.
.

Here, I want unnormalized output like

ID   |    items                       | name  |  loc  
______________________________________________________
id1  | item1                          | Mike  | CT
id1  | item2                          | Mike  | CT
id1  | item3                          | Mike  | CT
id1  | item4                          | Mike  | CT
id1  | item5                          | Mike  | CT
id2  | item3                          | Chris | MN
id2  | item7                          | Chris | MN
id2  | item4                          | Chris | MN
id2  | item9                          | Chris | MN
id2  | item8                          | Chris | MN

I am not a Hive SQL expert, Please help me out of this.

2
  • look at "explode" function in hive : cwiki.apache.org/confluence/display/Hive/… Commented Nov 20, 2014 at 12:45
  • Select explode(items) is working for one field only, how to fetch other columns with explode? Commented Nov 20, 2014 at 13:07

2 Answers 2

10

Try this:

 SELECT ID,itemsName,name,loc
 FROM Table
 LATERAL VIEW explode(items) itemTable AS itemsName;

in explode(items) , there items is your stored table column and Table is your Stored table.

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

2 Comments

Hi Kishore, Thanks for your reply. can we do the same with multiple array type columns? like ID | items | item_Name | name | loc ____________________________________________________________________ id1 | ["item1","item2","item3","item4","item5"] | ["Ruler","Cap","Pen","brush ","Eraser"] | Mike | CT id2 | ["item3","item7","item4","item9","item8"] | ["Pen","Pencil","brush"," ","Calc"] | Chris| MN
I am not getting your problem, make as another question and define question properly.
0

We can use the posexplode() function to achieve the scenario you mentioned, that is with multiple array columns.

Something like this will work out:

SELECT ID,i1.item,i2.itemName,name,loc
 FROM Table
 LATERAL VIEW posexplode(items) i1 AS item,item_1
 LATERAL VIEW posexplode(item_Name) i2 AS itemName,itemName_1
 WHERE item=itemName

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.