选择Hive结构中的所有列

8
我有一个要求,需要从hive结构中选择所有列。 下面是Hive创建表脚本。 创建表脚本 选择*从表中显示每个结构作为一列 select * from table
我的要求是在hive中将结构集合的所有字段显示为一列。
用户不应该逐个编写列名。 有没有UDF可以做到这一点?

请用包含一些数据样本的文本替换图片。 - David דודו Markovitz
3个回答

12

演示

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

数组
内联


这个方法在显示Hive结构的所有列方面非常有效。顺便说一下,非常感谢! - Ward W

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

0

你可以在表的顶部使用视图,或者根据你想要的模式将数据转储到其他表中。

视图的语法如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接