【问题标题】:How to union tables with different schema in HIVE?如何在 HIVE 中合并具有不同模式的表?
【发布时间】:2016-04-20 16:03:45
【问题描述】:

我在 HIVE 中有两个表:

  • 表 A,其中包含列“N”,其类型为数组
  • 表 B,其中“N”列未出现

表 A 和 B 都包含“C”列。

我想这样联合他们:

select g.* from 

(select N, C from A
union all
select null as N, C from B
) g;

但这会在 HIVE 中引发错误:

FAILED:...Schema of both sides of union should match: Column N is of type array<string> on first table and type void on second table.

所以,我尝试转换数据类型:

select g.* from 

(select N, C from A
union all
select cast(null as array) as N, C from B
) g;

"cannot recognize input near 'array' ')' 'as' in primitive type specification. 失败

我该如何解决这个问题?谢谢

【问题讨论】:

    标签: arrays hive hiveql union-all


    【解决方案1】:

    嗯。可能有一种更简单的方法,但我不确定如何在 Hive 中表达 NULL 数组常量。您可以为此使用 SQL:

    select g.*
    from (select N, C from A
          union all
          select A.N, C
          from B join
               A 
               on 1 = 0
         ) g;
    

    换句话说,我可能不知道如何表达我脑海中的常数。但是,我可以安排从A 获取它——通过未能匹配到一行。

    【讨论】:

    • 会因为join而变慢吗?
    • @Candic3 。 . .老实说,我不知道。其他数据库在连接方面会非常聪明,但 Hive 可能不会。您可以随时执行(select A.* from A limit 1) 之类的操作——这应该非常快。
    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 2022-01-22
    • 2020-08-27
    • 2021-02-05
    • 2013-07-26
    • 2013-04-09
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多