问题描述:

实际应用中,常常存在修改数据表结构的需求,比如:增加一个新字段。

如果使用如下语句新增列,可以成功添加列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. REPLACE COLUMNS can also be used to drop columns. For example, "ALTER TABLE test_change REPLACE COLUMNS (a int, b int);" will remove column 'c' from test_change's schema.

The PARTITION clause is available in Hive 0.14.0 and later; see Upgrading Pre-Hive 0.13.0 Decimal Columns for usage.

The CASCADE|RESTRICT clause is available in Hive 1.1.0. ALTER TABLE ADD|REPLACE COLUMNS 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 changes only to table metadata.

Add/Replace Columns
ALTER TABLE table_name
[PARTITION partition_spec] -- (Note: Hive 0.14.0 and later)
ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
[CASCADE|RESTRICT] -- (Note: Hive 1.1.0 and later)

最新文章

  1. keepalived mariadb 主主
  2. errorlevel
  3. 【转载】 Python 调整屏幕分辨率
  4. Educational Codeforces Round 16---部分题解
  5. Swipe2.1更新——移动Web内容滑块
  6. C++里容易忽视却不能忽视的
  7. linux修改时区,时间格式
  8. 本地连接速度100.0mbps变10.0mbps如何恢复
  9. TypeScript 素描 - 模块解析、声明合并
  10. SRM 585 DIV 1 总结
  11. mysql 数据库知识
  12. (五):C++分布式实时应用框架——支撑复杂的业务通讯关系
  13. AngularJS 最常用的几种功能
  14. Java开发笔记(十)一元运算符的技巧
  15. jcp 打印机字体变淡变模糊bootstrap
  16. Python paramiko模块基本使用(一)
  17. Xcode 运行时配置
  18. selenium之测试卫星资料页面操作(元素遍历)
  19. rest framework错误笔记——身份验证和权限
  20. jmeter之最佳实践

热门文章

  1. oracle drop 表后 恢复
  2. Git基本命令整理
  3. application.xml中配置文件properties导入
  4. js如何实现类的继承
  5. vue 修改 端口号
  6. Webpack学习-Loader
  7. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents
  8. nginx 实现所有的子域名301跳转到另外一个域名的对应子域名
  9. 小谈对Python的认知与期望
  10. Vue.directive添加全局指令详解