hive 添加,修改,删除字段的坑

 

alter table table_name add  columns(location_id string) ;

alter table table_name change column complete_status complete_status string;

使用alter table语句更新变结构时,由于默认更行现有表结构,对历史分区表元数据无改动,导致原有分区变数据,新字段为null,

hive 添加,修改,删除字段的坑

重新更新历史分区表也无动于衷,没有效果。

hive 1.1.0以前办法,

alter table table_name  partition(p='20190101') add  columns(location_id string) ;

alter table table_name partition(p='20190101') change column complete_status complete_status string;

通过添加partition一个一个分区修改 ,

hive 1.1.0后可以使用cascoda,在alter table语句后添加cascoda关键字执行,一劳永逸。

注意:CASCADE命令更改表更改列将更改表元数据的列,并将相同的更改级联到所有分区元数据

alter table table_name add  columns(location_id string) cascoda|restrict;

alter table table_name change column complete_status complete_status string cascoda|restrict;

官方解释:

The CASCADE|RESTRICT clause is available in Hive 1.1.0. ALTER TABLE CHANGE COLUMN with CASCADE command changes the columns of a table’s metadata, and cascades the same change to all the partition metadata. RESTRICT is the default, limiting column change only to table metadata.

ALTER TABLE CHANGE COLUMN CASCADE clause will override the table partition’s column metadata regardless of the table or partition’s protection mode. Use with discretion.

The column change command will only modify Hive’s metadata, and will not modify data. Users should make sure the actual data layout of the table/partition conforms with the metadata definition.