在配置单元中创建表时向列添加默认值

时间:2015-05-04 15:08:00

标签: hive hiveql

我能够从外部文件中的数据创建一个配置单元表。现在,我希望从上表中的数据创建另一个表,其中包含具有默认值的其他列。

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

2 个答案:

答案 0 :(得分:5)

您可以指定在create / update上从表中选择哪些列。只需提供默认值作为列之一。 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)

允许动态分区:

hive> SET hive.exec.dynamic.partition.mode=nonstrict;

创建第二个表:

hive> create table table2(col1 string, col2 string);

使用默认值从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)

答案 1 :(得分:2)

我也一直在寻找解决方案,并提出了以下建议:

CREATE TABLE test_table AS SELECT
 CASE 
  WHEN TRUE
  THEN "desired_value" 
 END AS default_column_name;