【问题标题】:Select all columns of a Hive Struct选择 Hive 结构的所有列
【发布时间】:2017-08-08 08:16:16
【问题描述】:

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

Hive 创建表脚本在下面

Create Table script

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

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

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

【问题讨论】:

  • 请用文字替换图片,包括一些数据样本

标签: struct hive udf apache-hive hive-udf


【解决方案1】:

演示

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

【讨论】:

  • 这可以很好地显示 Hive 结构的所有列,顺便说一句。非常感谢!
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      相关资源
      最近更新 更多