原文链接:http://blog.csdn.net/opensure/article/details/51323220
msck 命令也就是更新hive表的元数据。当表的分区命令存在,而在shell中看到不表的分区情况的时候,说明表的元数据没有更新,所以通msck表的操作可以更新表的元数据。
一、介绍
我们平时通常是通过alter table add partition方式增加Hive的分区的,但有时候会通过HDFS put/cp命令往表目录下拷贝分区目录,如果目录多,需要执行多条alter语句,非常麻烦。Hive提供了一个"Recover Partition"的功能。
具体语法如下:
- MSCK REPAIR TABLE table_name;
原理相当简单,执行后,Hive会检测如果HDFS目录下存在但表的metastore中不存在的partition元信息,更新到metastore中。
二、测试
- #当前没有partition元信息
- hive> show partitions cr_cdma_bsi_mscktest;
- OK
- Time taken: 0.104 seconds
- #创建两个分区目录
- hive> dfs -mkdir /user/hive/warehouse/cr_cdma_bsi_mscktest/month=201603;
- hive> dfs -mkdir /user/hive/warehouse/cr_cdma_bsi_mscktest/month=201604;
- #使用MSCK修复分区
- hive> msck repair table cr_cdma_bsi_mscktest;
- OK
- Partitions not in metastore: cr_cdma_bsi_mscktest:month=201603
- Partitions not in metastore: cr_cdma_bsi_mscktest:month=201604
- Repair: Added partition to metastore cr_cdma_bsi_mscktest:month=201603
- Repair: Added partition to metastore cr_cdma_bsi_mscktest:month=201604
- Time taken: 0.286 seconds, Fetched: 2 row(s)
- #再次查看,发现已经成功更新元信息
- hive> show partitions cr_cdma_bsi_mscktest;
- OK
- month=201603
- month=201604
- Time taken: 0.102 seconds, Fetched: 1 row(s)
相关JIRA:HIVE-874
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL