Hive添加列、修改列(调整位置)、删除列操作等

一时失言乱红尘 2022-12-06 01:24 572阅读 0赞

1.添加1列或多列

  1. # 添加1列
  2. alter table table_name add columns(
  3. user_id bigint comment '用户ID'
  4. );
  5. # 添加多列
  6. alter table table_name add columns(
  7. name string comment '用户名称',
  8. city string comment '城市',
  9. sex string comment '用户性别',
  10. age string comment '用户年龄',
  11. phone string comment '用户手机',
  12. email string comment '用户邮箱',
  13. unqiue_id string comment '身份证ID'
  14. );

2.修改列名/类型/位置/注释

  1. # 将性别(sex)名称改成gender,类型改成int,注释改成“性别”
  2. alter table table_name change sex gender int comment '性别';
  3. # 将age字段类型改为int,并将位置移动到name字段后面
  4. alter table table_name change age age int comment '用户年龄' after name;

3.删除列/更新列

  1. # 由于数据脱敏,我们需要删除unqiue_id列
  2. alter table table_name replace (
  3. user_id bigint comment '用户ID',
  4. name string comment '用户名称',
  5. city string comment '城市',
  6. sex string comment '用户性别',
  7. age string comment '用户年龄',
  8. phone string comment '用户手机',
  9. email string comment '用户邮箱'
  10. );
  11. # 相当于使用replace重新将表的列给更新替换了

4.重命名表名

  1. # 修改表名
  2. alter table table_name rename to table_name2;

5.限制表的查询和删除操作

  1. # 限制表的dt='2020-01-01'分区不能被drop掉
  2. ALTER TABLE table_name PARTITION(dt='2020-01-01') ENABLE NO_DROP;
  3. # 限制表的dt='2020-01-01'分区不能被查询
  4. ALTER TABLE table_name PARTITION(dt='2020-01-01') ENABLE OFFLINE;

发表评论

表情:
评论列表 (有 0 条评论,572人围观)

还没有评论,来说两句吧...

相关阅读