Select all columns of a Hive Struct
Asked Answered
J

3

8

I have a requirement to select * from all columns from a hive struct.

Hive create table script is here below

Create Table script

Select * from the table displays each struct as a column select * from table

The requirement i have is to display all fields of a struct collection as a column in hive.

The users shouldnt have to write column names individually. Does anyone have a UDF to do this?

Janenejanenna answered 16/3, 2017 at 22:54 Comment(1)
Please replace the pictures with text including some data sampleTedtedd
H
12

Demo

create table t 
(
    i   int
   ,s1  struct<id:int,birthday:date,fname:string>
   ,s2  struct<id:int,lname:string>
)
;

insert into t 
select  1
       ,named_struct('id',333,'birthday',date '1941-10-13','fname','Paul')
       ,named_struct('id',444,'lname','Simon')
;

insert into t 
select  2
       ,named_struct('id',777,'birthday',date '1941-11-05','fname','Art')
       ,named_struct('id',888,'lname','Garfunkel')
;

select * from t
;

+-----+---------------------------------------------------+--------------------------------+
| t.i |                       t.s1                        |              t.s2              |
+-----+---------------------------------------------------+--------------------------------+
|   1 | {"id":333,"birthday":"1941-10-13","fname":"Paul"} | {"id":444,"lname":"Simon"}     |
|   2 | {"id":777,"birthday":"1941-11-05","fname":"Art"}  | {"id":888,"lname":"Garfunkel"} |
+-----+---------------------------------------------------+--------------------------------+

select  i
       ,i1.*
       ,i2.*

from    t
        lateral view inline (array(s1)) i1 
        lateral view inline (array(s2)) i2
;

+---+-------+-------------+----------+-------+-----------+
| i | i1.id | i1.birthday | i1.fname | i2.id | i2.lname  |
+---+-------+-------------+----------+-------+-----------+
| 1 |   333 | 1941-10-13  | Paul     |   444 | Simon     |
| 2 |   777 | 1941-11-05  | Art      |   888 | Garfunkel |
+---+-------+-------------+----------+-------+-----------+

array
inline

Handrail answered 17/3, 2017 at 6:47 Comment(1)
This works wonderfully to display all columns of a Hive struct, BTW. Thanks a lot!Typhoon
P
1

Awesome! Thank you for this, I was looking for the same. Actually, it seems you can reuse the same column name.

select s1.*
from t
lateral view inline (array(s1)) s1
;

+-------+--------------+----------+
| s1.id | s1.birthday  | s1.fname |
+-------+--------------+----------+
| 333   | 10/13/1941   | Paul     |
| 777   | 11/5/1941    | Art      |
+-------+--------------+----------+
Peabody answered 8/7, 2020 at 14:4 Comment(0)
H
0

You may use view on top of table or dump the data in some other tables based on schema you want. Syntax for view :-

    create view foodmart.customerfs_view as select rcrm.customer_id .....  
from foodmart.customerfs_view
Haddock answered 17/3, 2017 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.