选择配置单元结构的所有列

lvmkulzt  于 2021-06-26  发布在  Hive
关注(0)|答案(3)|浏览(402)

我需要从配置单元结构的所有列中选择*。
配置单元创建表脚本如下
创建表脚本
selectfrom表格将每个结构显示为列selectfrom表格
我的要求是在配置单元中将结构集合的所有字段显示为列。
用户不必单独编写列名。有人有自定义项来做这个吗?

rggaifut

rggaifut1#

令人惊叹的!谢谢你,我也在找。实际上,似乎可以重用相同的列名。

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      |
+-------+--------------+----------+
gdx19jrr

gdx19jrr2#

您可以使用表顶视图,也可以根据所需的模式将数据转储到其他一些表中。视图的语法:-

create view foodmart.customerfs_view as select rcrm.customer_id .....  
from foodmart.customerfs_view
enxuqcxy

enxuqcxy3#

演示

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 |
+---+-------+-------------+----------+-------+-----------+

数组
内联

相关问题