删除存储为 orc 的 hive 表的列

Ann*_*Ann 6 hive

我创建了如下配置单元表:

create table alpha001(id int, name string) clustered by (id) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')
Run Code Online (Sandbox Code Playgroud)

现在我想删除其中一列,比如“名称”。我尝试了以下方法:

ALTER TABLE alpha001 REPLACE COLUMNS (id int);
Run Code Online (Sandbox Code Playgroud)

结果如下

Exception thrown: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Replace columns is not supported for table default.alpha001. SerDe may be incompatible.
Run Code Online (Sandbox Code Playgroud)

和以下

Exception thrown: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Replace columns is not supported for table default.alpha001. SerDe may be incompatible.
Run Code Online (Sandbox Code Playgroud)
Exception thrown : FAILED: ParseException line 1:26 mismatched input 'name' expecting PARTITION near 'DROP' in drop partition statement
Run Code Online (Sandbox Code Playgroud)

Rah*_*hul 5

不幸的是,你不能!从现有表中删除列的唯一方法是使用REPLACE COLUMNS关键字。但这只能用于具有本机 SerDe(DynamicSerDe、MetadataTypedColumnsetSerDe、LazySimpleSerDe 和 ColumnarSerDe)的表。

最好的办法是重新创建架构。按照步骤操作。

  1. 检查表是否是外部的。如果不是,请使用以下语句将其设置为外部。

    alter table alpha001 set tblproperties('EXTERNAL'='TRUE');
    
    Run Code Online (Sandbox Code Playgroud)
  2. 放下桌子。由于该表是外部表,您可以删除它而不删除实际表。

  3. 使用新架构重新创建表。您应该能够使用新架构访问该表。

遵循快速示例。

create table alpha001(id int, name string) clustered by (id) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true');

--assuming your table is not EXTERNAL already
alter table alpha001 set tblproperties('EXTERNAL'='TRUE');

insert into alpha001 values(1,"A");

select * from alpha001;
Run Code Online (Sandbox Code Playgroud)
OK
1       A
Run Code Online (Sandbox Code Playgroud)
drop table alpha001;

create table alpha001(id int) clustered by (id) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true');

select * from alpha001;
Run Code Online (Sandbox Code Playgroud)
OK
1
Time tak
Run Code Online (Sandbox Code Playgroud)

希望有帮助!

  • 这太令人难以置信了。光是删除一个列就太麻烦了。 (2认同)

Ank*_*ari 0

您不能删除配置单元表的列。在使用“ALTER TABLE alpha001 DROP name”时,它会给出错误,因为在配置单元中不支持删除列,但我们无法删除分区。因此,在这里,它在删除附近搜索分区名称,而“名称”列不支持。

因此,为了删除列,您必须1.删除表并重新创建删除了列的表(但如果其内部表可能会导致数据丢失)2.您可以使用REPLACE命令(请参阅此https://cwiki .apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-AlterTable/分区/列)