我有如下配置的 hive 表:

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

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

ALTER TABLE alpha001 REPLACE COLUMNS (id int);

结果如下
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.

和以下

ALTER TABLE alpha001 DROP name;
Exception thrown : FAILED: ParseException line 1:26 mismatched input 'name' expecting PARTITION near 'DROP' in drop partition statement

最佳答案

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

最好的选择是重新创建架构。遵循步骤。

  • 检查表是否在外部。如果不是,请使用以下语句将其设置为外部。
    alter table alpha001 set tblproperties('EXTERNAL'='TRUE');
    
  • 删除表。由于该表是外部表,因此可以删除它而不删除实际表。
  • 使用新架构重新创建表。您应该能够使用新的架构访问该表。

  • 进行快速采样。
    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;
    


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



    希望对您有所帮助!

    关于hive - 配置为ORC的Hive表的删除列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32068903/

    10-11 10:18
    查看更多