Hive 修复分区 MSCK REPAIR TABLE的使用

因为昨天工作的时候踩 了坑,所以来记录一下。(我的问题是:我把hive表手动删掉 ,后来重新创建了一个一样的表,然后原有的分区数据全部损坏了,数据导不进去了)

一、msck repair table 命令是做啥的

MSCK REPAIR TABLE命令主要是用来:解决通过hdfs dfs -put或者hdfs api写入hive分区表的数据在hive中无法被查询到的问题。

我们知道hive有个服务叫metastore,这个服务主要是存储一些元数据信息,比如数据库名,表名或者表的分区等等信息。如果不是通过hive的insert等插入语句,很多分区信息在metastore中是没有的,如果插入分区数据量很多的话,你用 ALTER TABLE table_name ADD PARTITION 一个个分区添加十分麻烦。这时候MSCK REPAIR TABLE就派上用场了。只需要运行MSCK REPAIR TABLE命令,hive就会去检测这个表在hdfs上的文件,把没有写入metastore的分区信息写入metastore。

二、例子

我们先创建一个分区表,然后往其中的一个分区插入一条数据,在查看分区信息

-- 建分区表
CREATE TABLE repair_test (col_a STRING)
PARTITIONED BY (par STRING);

-- 插入数据
INSERT INTO TABLE repair_test PARTITION(par="partition_1") VALUES ("test");

-- 查看分区信息
SHOW PARTITIONS repair_test;

 查看分区信息的结果如下:

+------------------+--+
| partition |
+------------------+--+
| par=partition_1 |
+------------------+--+
1 row selected (0.073 seconds)
0: jdbc:hive2://localhost:10000>

然后我们通过hdfs的put命令手动创建一个数据

[ericsson@h3cnamenode1 pcc]$ echo "123123" > test.txt

[ericsson@h3cnamenode1 pcc]$ hdfs dfs -mkdir -p /user/hive/warehouse/test.db/repair_test/par=partition_2/

[ericsson@h3cnamenode1 pcc]$ hdfs dfs -put -f test.txt /user/hive/warehouse/test.db/repair_test/par=partition_2/

[ericsson@h3cnamenode1 pcc]$ hdfs dfs -ls -R /user/hive/warehouse/test.db/repair_test

drwxrwxrwt - ericsson hive 0 2018-08-10 17:46 /user/hive/warehouse/test.db/repair_test/par=partition_1

drwxrwxrwt - ericsson hive 0 2018-08-10 17:46 /user/hive/warehouse/test.db/repair_test/par=partition_1/.hive-staging_hive_2018-08-10_17-45-59_029_1594310228554990949-1

drwxrwxrwt - ericsson hive 0 2018-08-10 17:46 /user/hive/warehouse/test.db/repair_test/par=partition_1/.hive-staging_hive_2018-08-10_17-45-59_029_1594310228554990949-1/-ext-10000

-rwxrwxrwt 3 ericsson hive 5 2018-08-10 17:46 /user/hive/warehouse/test.db/repair_test/par=partition_1/000000_0

drwxr-xr-x - ericsson hive 0 2018-08-10 17:57 /user/hive/warehouse/test.db/repair_test/par=partition_2

-rw-r--r-- 3 ericsson hive 7 2018-08-10 17:57 /user/hive/warehouse/test.db/repair_test/par=partition_2/test.txt

[ericsson@h3cnamenode1 pcc]$

这时候我们查询分区信息,发现partition_2这个分区并没有加入到hive中

0: jdbc:hive2://localhost:10000> show partitions repair_test;
INFO : OK
+------------------+--+
| partition |
+------------------+--+
| par=partition_1 |
+------------------+--+
1 row selected (0.079 seconds)
0: jdbc:hive2://localhost:10000>

运行MSCK REPAIR TABLE 命令后再查询分区信息,可以看到通过put命令放入的分区已经可以查询了

0: jdbc:hive2:> MSCK REPAIR TABLE repair_test;
0: jdbc:hive2:> show partitions repair_test;
+------------------+--+
| partition |
+------------------+--+
| par=partition_1 |
| par=partition_2 |
+------------------+--+
2 rows selected (0.088 seconds)
0: jdbc:hive2:> select * from repair_test;
+--------------------+------------------+--+
| repair_test.col_a | repair_test.par |
+--------------------+------------------+--+
| test | partition_1 |
| 123123 | partition_2 |
+--------------------+------------------+--+
2 rows selected (0.121 seconds)
0: jdbc:hive2://localhost:10000>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值