场景:
如果事先建立了一张分区表,然后手动(比如使用 cp 或者 mv )将分区数据拷贝到刚刚新建的表进行数据初始化;但是对于分区表,需要在hive里面手动将刚刚初始化的数据分区加入到hive里面,这样才能够查询使用。通常的做法是使用 alter table add partition命令手动添加分区;但是如果初始化的分区太多,这样一条一条地手动添加分区不免过于麻烦(虽然可以写个脚本生成添加分区的命令)。
这时候,MSCK命令就派上用场了。
语法:
实例:
1、建表
为了让MSCK命令工作,分区的目录名必须是【 /partition_name=partition_value/】结构的( 且表级目录中不得存在非此种格式的目录名),否则将无法添加分区。这时候就必须使用add partition命令了。
如果事先建立了一张分区表,然后手动(比如使用 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命令了。