8

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

Hive 创建表脚本在下面

创建表脚本

select * from table 将每个结构显示为列 select * from table

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

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

4

3 回答 3

11

演示

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

数组
内联

于 2017-03-17T06:47:32.077 回答
1

惊人的!谢谢你,我也在寻找同样的东西。实际上,您似乎可以重用相同的列名。

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      |
+-------+--------------+----------+
于 2020-07-08T14:04:34.433 回答
0

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

    create view foodmart.customerfs_view as select rcrm.customer_id .....  
from foodmart.customerfs_view
于 2017-03-17T04:38:12.187 回答