在Hive中创建表时向列添加默认值

Arn*_*nab 4 hive hiveql

我可以根据外部文件中的数据创建配置单元表。现在,我希望根据上一个表中的数据创建另一个表,并使用默认值添加其他列。

我了解可以使用CREATE TABLE AS SELECT,但是如何添加具有默认值的其他列?

arg*_*ype 5

您可以指定在创建/更新时从表中选择哪些列。只需提供默认值作为列之一即可。下面是带有UPDATE的示例:

创建简单表并为其填充值:

hive> create table table1(col1 string);
hive> insert into table table1 values('val1');
hive> select col1 from table1;
OK
val1
Time taken: 0.087 seconds, Fetched: 1 row(s)
Run Code Online (Sandbox Code Playgroud)

允许动态分区:

hive> SET hive.exec.dynamic.partition.mode=nonstrict;
Run Code Online (Sandbox Code Playgroud)

创建第二个表:

hive> create table table2(col1 string, col2 string);
Run Code Online (Sandbox Code Playgroud)

使用默认值从table1填充它:

hive> insert overwrite table table2 select col1, 'DEFAULT' from table1;
hive> select * from table2;
OK
val1    DEFAULT
Time taken: 0.081 seconds, Fetched: 1 row(s)
Run Code Online (Sandbox Code Playgroud)