问题描述:
实际应用中,常常存在修改数据表结构的需求,比如:增加一个新字段。
如果使用如下语句新增列,可以成功添加列col1。但如果数据表tb已经有旧的分区(例如:dt=20190101),则该旧分区中的col1将为空且无法更新,即便insert overwrite该分区也不会生效。
alter table tb add columns(col1 string);
解决方法:
解决方法很简单,就是增加col1时加上cascade关键字。示例如下:
alter table tb add columns(col1 string) cascade;
加深记忆的方法也很简单,cascade的中文翻译为“级联”,也就是不仅变更新分区的表结构(metadata),同时也变更旧分区的表结构。
附:官方文档
ADD COLUMNS lets you add new columns to the end of the existing columns but before the partition columns. This is supported for Avro backed tables as well, for Hive 0.14 and later.
REPLACE COLUMNS removes all existing columns and adds the new set of columns. This can be done only for tables with a native SerDe (DynamicSerDe, MetadataTypedColumnsetSerDe, LazySimpleSerDe and ColumnarSerDe). Refer to Hive SerDe for more information. R

本文阐述了在Hive中修改数据表结构时常见的问题,特别是新增列时旧分区数据无法更新的情况。通过引入cascade关键字,可以有效解决此问题,确保新列在所有分区中正确初始化。
最低0.47元/天 解锁文章
3987

被折叠的 条评论
为什么被折叠?



