选择Hive Struct的所有列

Abh*_*yak 6 struct hive hive-udf udf apache-hive

我需要从hive结构中的所有列中选择*.

Hive创建表脚本如下所示

创建表脚本

从表中选择*将每个结构显示为从表中选择*

我的要求是将结构集合的所有字段显示为配置单元中的列.

用户不必单独编写列名.有没有人有UDF这样做?

Dav*_*itz 5

演示

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')
;
Run Code Online (Sandbox Code Playgroud)
select * from t
;
Run Code Online (Sandbox Code Playgroud)
+-----+---------------------------------------------------+--------------------------------+
| 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"} |
+-----+---------------------------------------------------+--------------------------------+
Run Code Online (Sandbox Code Playgroud)
select  i
       ,i1.*
       ,i2.*

from    t
        lateral view inline (array(s1)) i1 
        lateral view inline (array(s2)) i2
;
Run Code Online (Sandbox Code Playgroud)
+---+-------+-------------+----------+-------+-----------+
| 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 |
+---+-------+-------------+----------+-------+-----------+
Run Code Online (Sandbox Code Playgroud)

数组
内联


小智 0

您可以在表顶部使用视图,或者根据您想要的架构将数据转储到其他一些表中。视图语法:-

    create view foodmart.customerfs_view as select rcrm.customer_id .....  
from foodmart.customerfs_view
Run Code Online (Sandbox Code Playgroud)