hive在修改表的字段类型时使用语法:
ALTER TABLE table_name PARTITION partition_spec]
CHANGE COLUMN] col_old_name col_new_name column_type COMMENT col_comment]
FIRST|AFTER column_name] CASCADE|RESTRICT];
--新增字段
alter table t66_cust_event add columns (oppp_cust_id string);
--修改类型及增加注释
alter table t66_cust_event change column oppp_cust_id oppp_cust_id_2 string comment 'test';
--移动字段顺序
alter table t66_cust_event change column oppp_cust_id oppp_cust_id_2 string comment 'test'
after col1;
PS:移动操作须针对已存在的字段,在字段新增的时候,不能直接和移动操作一起
此操作在修改已有数据的表时,会报错,因为修改元数据,并未修改原始数据
官方解释:
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.
列更改命令将只修改Hive的元数据,而不修改数据。用户应该确保表/分区的实际数据布局符合元数据定义。
--在删除一个或多个字段之后,原始数据的原始字段的值不会随之丢失(要区分数据与元数据)
// 删除字段(使用新schema替换原有的)
ALTER TABLE t66_cust_event REPLACE COLUMNS(id BIGINT, name STRING ......);
^ - ^ ……