Hive分区修复命令MSCK使用1

场景:
如果事先建立了一张分区表,然后手动(比如使用 cp 或者 mv )将分区数据拷贝到刚刚新建的表进行数据初始化;但是对于分区表,需要在hive里面手动将刚刚初始化的数据分区加入到hive里面,这样才能够查询使用。通常的做法是使用 alter table add partition命令手动添加分区;但是如果初始化的分区太多,这样一条一条地手动添加分区不免过于麻烦(虽然可以写个脚本生成添加分区的命令)。
这时候,MSCK命令就派上用场了。
语法:
MSCK REPAIR TABLE table_name;
执行后,Hive会检测如果HDFS目录下存在但表的metastore中不存在的partition元信息,更新到metastore中。
实例:
1、建表
CREATE TABLE `xxxxxx_uid_online`(
  `datehour` string, 
  `halfhourtype` string, 
  `uid` string, 
  `roomid` string, 
  `roomcreatoruid` string, 
  `staytime` string)
PARTITIONED BY ( 
  `pt_day` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online'
TBLPROPERTIES (
  'transient_lastDdlTime'='1516865482')
2、查看分区信息
hive> show partitions xxxxxx_uid_online;
OK
Time taken: 0.048 seconds
3、复制数据
hadoop  distcp  hdfs://10.28.28.177:9000/user/hive/warehouse/xxxxxx_uid_online/2018-01-01 hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online/pt_day=2018-01-01
hadoop  distcp  hdfs://10.28.28.177:9000/user/hive/warehouse/xxxxxx_uid_online/2017-12-31 hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online/pt_day=2017-12-31
hadoop  distcp  hdfs://10.28.28.177:9000/user/hive/warehouse/xxxxxx_uid_online/2017-12-30 hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online/pt_day=2017-12-30
hadoop  distcp  hdfs://10.28.28.177:9000/user/hive/warehouse/xxxxxx_uid_online/2017-12-29 hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online/pt_day=2017-12-29
hadoop  distcp  hdfs://10.28.28.177:9000/user/hive/warehouse/xxxxxx_uid_online/2017-12-28 hdfs://emr-cluster/user/hive/warehouse/xxxxxx_uid_online/pt_day=2017-12-28
4、执行修复命令
hive> MSCK REPAIR TABLE xxxxxx_uid_online;  
OK
Partitions not in metastore:    xxxxxx_uid_online:pt_day=2017-12-28     xxxxxx_uid_online:pt_day=2017-12-29     xxxxxx_uid_online:pt_day=2017-12-30     xxxxxx_uid_online:pt_day=2017-12-31
Repair: Added partition to metastore xxxxxx_uid_online:pt_day=2017-12-28
Repair: Added partition to metastore xxxxxx_uid_online:pt_day=2017-12-29
Repair: Added partition to metastore xxxxxx_uid_online:pt_day=2017-12-30
Repair: Added partition to metastore xxxxxx_uid_online:pt_day=2017-12-31
Time taken: 0.133 seconds, Fetched: 5 row(s)
如果目录格式不对,则会报错:
hive> MSCK REPAIR TABLE xxxxxx_uid_online;  
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
5、查看分区信息
hive> show partitions xxxxxx_uid_online;
OK
pt_day=2017-12-28
pt_day=2017-12-29
pt_day=2017-12-30
pt_day=2017-12-31
pt_day=2018-01-01
Time taken: 0.039 seconds, Fetched: 5 row(s)
注意:
为了让MSCK命令工作,分区的目录名必须是【/partition_name=partition_value/】结构的(且表级目录中不得存在非此种格式的目录名),否则将无法添加分区。这时候就必须使用add partition命令了。
--------------------- 
作者:BabyFish13 
来源:CSDN 
原文:https://blog.csdn.net/BabyFish13/article/details/79169496 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值