MSCK REPAIR TABLEMSCK REPAIR TABLE

恢复表目录中的所有分区并更新 Hive 元存储。Recovers all the partitions in the directory of a table and updates the Hive metastore. 使用 PARTITIONED BY 子句创建表时,将在 Hive 元存储中生成和注册分区。When creating a table using PARTITIONED BY clause, partitions are generated and registered in the Hive metastore. 但是,如果已分区表是根据现有数据创建的,则不会在 Hive 元存储中自动注册分区;必须运行 MSCK REPAIR TABLE 来注册分区。However, if the partitioned table is created from existing data, partitions are not registered automatically in the Hive metastore; you must run MSCK REPAIR TABLE to register the partitions. 对于不存在的表或不具有分区的表,MSCK REPAIR TABLE 将引发异常。MSCK REPAIR TABLE on a non-existent table or a table without partitions throws an exception. 恢复分区的另一种方法是使用 ALTER TABLE RECOVER PARTITIONSAnother way to recover partitions is to use ALTER TABLE RECOVER PARTITIONS.

语法Syntax

MSCK REPAIR TABLE table_identifier

参数Parameters

  • table_identifiertable_identifier

    要修复的表的名称。The name of the table to be repaired. 可以选择使用数据库名称来限定表名称。The table name may be optionally qualified with a database name.

    语法: [database_name.] table_nameSyntax: [database_name.] table_name

示例Examples

-- create a partitioned table from existing data /tmp/namesAndAges.parquet
CREATE TABLE t1 (name STRING, age INT) USING parquet PARTITIONED BY (age)
    LOCATION "/tmp/namesAndAges.parquet";

-- SELECT * FROM t1 does not return results
SELECT * FROM t1;

-- run MSCK REPAIR TABLE to recovers all the partitions
MSCK REPAIR TABLE t1;

-- SELECT * FROM t1 returns results
SELECT * FROM t1;
+-------+---+
|   name|age|
+-------+---+
|Michael| 20|
+-------+---+
| Justin| 19|
+-------+---+
|   Andy| 30|
+-------+---+