在Hive的分区级别添加列

时间:2018-09-27 13:44:51

标签: hadoop hive hiveql

对于Hive来说是新手,我们需要将列添加到现有的Hive表中。 我是在以下命令的帮助下完成的。 更改表tableName添加列(colName数据类型)级联;

但是在hive文档中,我们使用alter命令在分区级别添加列。 我尝试了以下命令。

hive> SET hive.exec.dynamic.partition = true;
hive> alter table test_alter_col partition(c=1) add columns (d1 int);
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Duplicate column name: d1
hive> select d1 from test_alter_col where c=1;
FAILED: SemanticException [Error 10004]: Line 1:7 Invalid table alias or column reference 'd1': (possible column names are: a1, b1, d, c)
hive> alter table test_alter_col partition(c=1) add columns (d2 int);
OK
Time taken: 0.178 seconds
hive> select d2 from test_alter_col where c=1;
FAILED: SemanticException [Error 10004]: Line 1:7 Invalid table alias or column reference 'd2': (possible column names are: a1, b1, d, c)
hive>

以上命令的作用是什么?在分区级别使用alter命令有什么用例。

编辑1-

也尝试过以下命令,但仍然无法查询新添加的列或插入数据。

create table test_partn (a int, b int, c int) partitioned by (d int) row format delimited fields terminated by '\t' stored as textfile;

insert into table test_partn partition(d) values (1, 11, 111, 1111), (2, 22, 222, 2222), (3, 33, 333, 3333);

SET hive.exec.dynamic.partition = true;

alter table test_partn partition(d=1111) add columns (e int);
insert into test_partn partition(d=1111) values (1, 12, 13, 14);
FAILED: SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different '1111': Table insclause-0 has 3 columns, but query has 4 columns.

alter table test_partn partition(d=3333) add columns (e int) restrict;
insert into test_partn partition(d=3333) values (1, 12, 13, 14);

谢谢, 维杰

2 个答案:

答案 0 :(得分:1)

  

hive>修改表test_alter_col分区(c = 1)添加列(d1 int);   失败:执行错误,从org.apache.hadoop.hive.ql.exec.DDLTask返回代码1。列名称重复:d1   h

关于您的第一个命令,您的配置单元表中似乎已经有重复的列名。您需要使用其他列。

或者,如果要向已分区的配置单元表中添加列,则可以使用以下命令:

ALTER TABLE <table name> ADD columns (column1 string) CASCADE;

以上内容应完成将列添加到已分区表中的工作。此处捕获的是CASCADE关键字,它将对配置单元中所有分区的更改进行级联。

希望这会有所帮助:)

答案 1 :(得分:0)

我认为在分区级别添加列只会在元数据级别添加列名称。

我尝试查询-describe extended tablename partition (keycol=value)

下面是结果。

hive> describe extended test_partn partition(d=1111);
OK
a                       int
b                       int
c                       int
e                       int
d                       int

# Partition Information
# col_name              data_type               comment

d                       int

Detailed Partition Information  Partition(values:[1111], dbName:test, tableName:test_partn, createTime:1539261860, lastAccessTime:0, sd:StorageDescriptor(cols:[FieldSchema(name:a, type:int, comment:null), FieldSchema(name:b, type:int, comment:null), FieldSchema(name:c, type:int, comment:null), FieldSchema(name:e, type:int, comment:null), FieldSchema(name:d, type:int, comment:null)], location:maprfs:/user/hive/warehouse/test.db/test_partn/d=1111, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{serialization.format=     , field.delim=
Time taken: 0.139 seconds, Fetched: 12 row(s)
hive>

我只能看到该分区的新添加的列