hive中字段的替换replace的效果演示

191 阅读1分钟
字段的替换: alter table 表名 replace columns (字段名 字段类型 , ...);

先看替换前原表
select * from db_0806.products;
的情况如下:

image.png

执行:

alter table db_0806.products replace columns (
    name string,
    price int
    );

则将会报错:Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Unable to alter table. The following columns have types incompatible with the existing columns in their respective positions : price

说的是列 price 的类型与现有表中相应位置的列类型不兼容。

将列类型都改为string之后,执行成功,但是select * from db_0806.products;会发现结果如下:

image.png

其实结果很好理解,alter table更改的是表的元数据信息,hive将新的元数据与hdfs的数据进行映射成新的表,所以列中数据的顺序还是从原来的第一列开始,毕竟hdfs中实际block块数据是没有变化的。

所以说,replace更改字段既不能更改列的顺序,也需要适配列中数据的实际类型,实际应用场景不大。