1

I have a HIVE Table with following schema like this:

hive>desc books;
gen_id                  int                                         
author                  array<string>                               
rating                  double                               
genres                  array<string>  

hive>select * from books;

| gen_id         | rating    | author          |genres
+----------------+-------------+---------------+----------
| 1              | 10        | ["A","B"]       | ["X","Y"]  
| 2              | 20        | ["C","A"]       | ["Z","X"]
| 3              | 30        | ["D"]           | ["X"]

Is there a query where I can perform some SELECT operation and that returns individual rows, like this:

| gen_id      |  rating        | SplitData
+-------------+---------------+-------------
| 1           | 10            | "A"
| 1           | 10            | "B"
| 1           | 10            | "X"
| 1           | 10            | "Y"
| 2           | 20            | "C"
| 2           | 20            | "A"
| 2           | 20            | "Z"
| 2           | 20            | "X"
| 3           | 30            | "D"
| 3           | 30            | "X"

Can someone guide me how can get to this result. Thanks in advance for any kind of help.

1 Answer 1

3

You need to do Lateral view and explode,i.e.

SELECT
    gen_id,
    rating,
    SplitData 
FROM (
    SELECT  
       gen_id,
       rating, 
       array (ex_author,ed_genres) AS ar_SplitData  
    FROM 
       books 
       LATERAL VIEW explode(books.author) exploded_authors AS ex_author
       LATERAL VIEW explode(books.genres) exploded_genres AS ed_genres  
) tab
LATERAL VIEW explode(tab.ar_SplitData) exploded_SplitData AS SplitData;

I had no chance to test it but it should show you general path. GL!

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.