1 增加分区
1.1 添加一个分区
ALTER TABLE t_user_province ADD PARTITION (province='BJ') location
'/user/hive/warehouse/test.db/t_user_province/province=BJ';
必须自己把数据加载到增加的分区中 hive不会帮你添加
1.2 一次添加多个分区
ALTER TABLE table_name ADD PARTITION (dt='2008-08-08', country='us') location '/path/to/us/part080808'
PARTITION (dt='2008-08-09', country='us') location '/path/to/us/part080809';
2 重命名分区
ALTER TABLE t_user_province PARTITION (province ="SH") RENAME TO PARTITION (province ="Shanghai");
3 删除分区
ALTER TABLE table_name DROP [IF EXISTS] PARTITION (dt='2008-08-08', country='us');
直接删除数据 不进垃圾桶
ALTER TABLE table_name DROP [IF EXISTS] PARTITION (dt='2008-08-08', country='us') PURGE; --直接删除数据 不进垃圾桶
4 修改分区
4.1 更改分区文件存储格式
ALTER TABLE table_name PARTITION (dt='2008-08-09') SET FILEFORMAT file_format;
4.2 更改分区位置
ALTER TABLE table_name PARTITION (dt='2008-08-09') SET LOCATION "new location";
5 修复分区
MSCK [REPAIR] TABLE table_name [ADD/DROP/SYNC PARTITIONS];
--Step1:创建分区表
create table t_all_hero_part_msck(
id int,
name string,
hp_max int,
mp_max int,
attack_max int,
defense_max int,
attack_range string,
role_main string,
role_assist string
) partitioned by (role string)
row format delimited
fields terminated by "\t";
--Step2:在linux上,使用HDFS命令创建分区文件夹
hadoop fs -mkdir -p /user/hive/warehouse/test.db/t_all_hero_part_msck/role=sheshou
hadoop fs -mkdir -p /user/hive/warehouse/test.db/t_all_hero_part_msck/role=tanke
--Step3:把数据文件上传到对应的分区文件夹下
hadoop fs -put archer.txt /user/hive/warehouse/test.db/t_all_hero_part_msck/role=sheshou
hadoop fs -put tank.txt /user/hive/warehouse/test.db/t_all_hero_part_msck/role=tanke
--Step4:查询表 可以发现没有数据
select * from t_all_hero_part_msck;
--Step5:使用MSCK命令进行修复
--add partitions可以不写 因为默认就是增加分区
MSCK repair table t_all_hero_part_msck add partitions;
--Step1:直接使用HDFS命令删除分区表的某一个分区文件夹
hadoop fs -rm -r /user/hive/warehouse/test.db/t_all_hero_part_msck/role=sheshou
--Step2:查询发现还有分区信息
--因为元数据信息没有删除
show partitions t_all_hero_part_msck;
--Step3:使用MSCK命令进行修复
MSCK repair table t_all_hero_part_msck drop partitions;