【问题标题】:HIVE: How create a table with all columns in another table EXCEPT one of them?HIVE:如何创建一个表,其中包含另一个表中的所有列,但其中一个除外?
【发布时间】:2017-04-26 18:00:20
【问题描述】:

当我需要将列更改为分区 (convert normal column as partition column in hive) 时,我想创建一个新表来复制除一个之外的所有列。我目前在原始表中有 >50 列。有什么干净的方法吗?

类似:

CREATE student_copy LIKE student EXCEPT age and hair_color;

谢谢!

【问题讨论】:

  • 您也想移动数据吗?或者干脆创建一个空表?
  • 也想复制数据新建一个分区

标签: hadoop hive


【解决方案1】:

您可以使用正则表达式: CTAS using REGEX column spec.

set hive.support.quoted.identifiers=none;
CREATE TABLE student_copy AS SELECT `(age|hair_color)?+.+` FROM student;
set hive.support.quoted.identifiers=column;

但是(如 Kishore Kumar Suthar 所述: 这不会创建分区表,因为 CTAS 不支持(将表创建为选择)。

我认为获取分区表的唯一方法是获取表的完整创建语句(如 Abraham 所述):

SHOW CREATE TABLE student;

更改它以在您想要的列上创建一个分区。之后,您可以在插入新表时使用带有正则表达式的选择。 如果您的分区列已经是此选择的一部分,那么您需要确保它是last column you insert。如果不是,您可以在正则表达式中排除该列并将其包含在最后。此外,如果您希望根据插入语句创建多个分区,则需要启用“动态分区”:

set hive.support.quoted.identifiers=none;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
INSERT INTO TABLE student_copy PARTITION(partcol1) SELECT `(age|hair_color|partcol1)?+.+`, partcol1 FROM student;
set hive.support.quoted.identifiers=column;

需要“hive.support.quoted.identifiers=none”才能在查询的正则表达式部分使用反引号“`”。我在声明后将此参数设置为原始值:'hive.support.quoted.identifiers=column'

【讨论】:

    【解决方案2】:
    CREATE TABLE student_copy LIKE student;
    

    它只是复制源表定义。

    CREATE TABLE student_copy AS select name, age, class from student;
    
    • 目标不能是分区表。
    • 目标不能是外部表。
    • 它复制结构和数据

    【讨论】:

      【解决方案3】:

      我使用下面的命令来获取现有表的创建语句。

      SHOW CREATE TABLE student;
      

      复制结果并根据您对新表的要求进行修改,然后运行修改后的命令以获取新表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多