gpt4 book ai didi

struct - 选择 Hive 结构的所有列

转载 作者:行者123 更新时间:2023-12-04 12:17:29 27 4
gpt4 key购买 nike

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

Hive 创建表脚本在下面

Create Table script

Select * from the table 将每个结构体显示为一列
select * from table

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

用户不应该单独写列名。有没有人有 UDF 来做到这一点?

最佳答案

演示

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

关于struct - 选择 Hive 结构的所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com