最近在使用Hive的过程中,在备份数据时,经常会使用cp或mv命令来拷贝数据,将数据拷贝到我们新建备份表的目录下面,如果不是分区表,则上面的操作之后,新建的备份表可以正常使用,但是如果是分区表的,一般都是使用alter table add partition命令将分区信息添加到新建的表中,每添加一条分区信息就执行一个alter table add partition命令,如果分区数量少还好办,但是遇到分区数量多的情况,特别是分区数量大于50的情况,如果还是使用alter命令添加分区,那是一件耗时耗力的事情,还容易出错。

幸运的是Hive提供了MSCK命令,用于修复表的分区,我们先看Hive官方是如果介绍MCSK命令的:

Hive stores a list of partitions for each table in its metastore. If, however, new partitions are directly added to HDFS (say by using hadoop fs -put command), the metastore (and hence Hive) will not be aware of these partitions unless the user runs ALTER TABLE table_name ADD PARTITION commands on each of the newly added partitions.

However, users can run a metastore check command with the repair table option:

MSCK REPAIR TABLE table_name;

which will add metadata about partitions to the Hive metastore for partitions for which such metadata doesn't already exist. In other words, it will add any partitions that exist on HDFS but not in metastore to the metastore. See HIVE-874 for more details. When there is a large number of untracked partitions, there is a provision to run MSCK REPAIR TABLE batch wise to avoid OOME. By giving the configured batch size for the property hive.msck.repair.batch.size it can run in the batches internally. The default value of the property is zero, it means it will execute all the partitions at once.

The equivalent command on Amazon Elastic MapReduce (EMR)'s version of Hive is:

ALTER TABLE table_name RECOVER PARTITIONS;

Starting with Hive 1.3, MSCK will throw exceptions if directories with disallowed characters in partition values are found on HDFS. Use hive.msck.path.validation setting on the client to alter this behavior; "skip" will simply skip the directories. "ignore" will try to create partitions anyway (old behavior). This may or may not work.

翻译成中文的大概意思就是:Hive将每个表的分区信息保存在metastore中,如果通过hadoop fs -put命令直接将分区信息添加到HDFS,metastore是不会感知到这些新增的分区,除非执行了ALTER TABLE table_name ADD PARTITION命令。但是用户可以运行metastore检查命令MSCK REPAIR TABLE table_name;该命令将关于分区的元信息添加到Hive metastore中,这是对于那些没有元信息的分区来说的。换句话说,就是将任何存在于HDFS上但不在metastore上的分区添加到metastore。

下面介绍如果使用MSCK命令,创建了一个bigdata17_partition表,然后通过cp命令将几个目录的文件拷贝到bigdata17_partition目录下面,然后执行show partitions bigdata17_partition命令,将不会显示分区的信息:

hive> show partitions bigdata17_partition;
OK
Time taken: 1.121 seconds

然后执行MSCK REPAIR TABLE bigdata17_partition;命令添加分区:

hive> MSCK REPAIR TABLE bigdata17_partition;
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 10124
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 21234
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 346783
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 2532162
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 2901198
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 3129087
18/10/11 17:27:15 WARN log: Updating partition stats fast for: bigdata17_partition;
18/10/11 17:27:15 WARN log: Updated size to 23190876
OK
Partitions not in metastore: bigdata17_partition;:dt=2018-09-15 bigdata17_partition;:dt=2018-09-16 bigdata17_partition;:dt=2018-09-17 bigdata17_partition;:dt=2018-09-18 bigdata17_partition;:dt=2018-09-19 bigdata17_partition;:dt=2018-09-20 bigdata17_partition;:dt=2018-09-21
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-15
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-16
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-17
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-18
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-19
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-20
Repair: Added partition to metastore bigdata17_partition;:dt=2018-09-21
Time taken: 0.613 seconds, Fetched 8 row(s)

通过上述的结果可以看到已经将bigdata17_partition表的分区信息添加到Hive metastore中,和add partition命令比起来既方便又高效。

有点需要注意的是,分区的目录结构必遵循

/partition_name=partition_value/结构,否则msck无法自动添加分区,只能使用add partition命令。

一起学Hive——使用MSCK命令修复Hive分区的更多相关文章

  1. 使用MSCK命令修复Hive表分区

    set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...

  2. shell命令执行hive脚本(hive交互,hive的shell编程)

    Hive执行方式 Hive的hql命令执行方式有三种: 1.CLI 方式直接执行 2.作为字符串通过shell调用hive –e执行(-S开启静默,去掉"OK","Tim ...

  3. 二、hive shell常用命令

    在使用hive shell之前我们需要先安装hive,并启动hdfs 请参考:https://www.cnblogs.com/lay2017/p/9973298.html hive shell 我们先 ...

  4. hive经常使用命令

    hive经常使用命令 show tables; 列出hive里面全部数据表名 desc userProfile; 显示数据表userProfile的基本表字段及字段type desc extended ...

  5. Hive的Explain命令

    Hive的Explain命令,用于显示SQL查询的执行计划. Hive查询被转化成序列阶段(这是一个有向无环图).这些阶段可能是mapper/reducer阶段,或者是Metastore或文件系统的操 ...

  6. hive的desc命令

    desc命令 desc 命令是为了展示hive表格的内在属性.例如列名,data_type,存储位置等信息.这个命令常常用在我们对hive表格观察之时,我们想要知道这个hive各个列名(基于这些具体列 ...

  7. Hive 系列(三)—— Hive CLI 和 Beeline 命令行的基本使用

    一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...

  8. Hive相关的命令

    hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低,可以通过 ...

  9. hive的常用命令

    #从hive中直接进入hdfs的daas/bstl/term/userinfo目录下 hive> !hadoop fs -ls /daas/bstl/term/userinfo; 查看hive表 ...

随机推荐

  1. MySQL联结查询和子查询

    2018-2-24 16:18:12 星期六 今天需要统计一个运营活动的数据, 涉及三个表, 分组比较多 活动描述: 每个人可以领取多张卡片,  好友也可以赠送其卡片, 20或40张卡片可以兑换一个奖 ...

  2. ASP.NET提供三种主要形式的缓存

    ASP.NET提供三种主要形式的缓存:页面级输出缓存.用户控件级输出缓存(或称为片段缓存)和缓存API.

  3. Go学习笔记(只有链接)

    Go学习笔记 link: https://blog.csdn.net/u011304970/article/details/69908641 仅作为记录使用.

  4. IntelliJ IDEA插件 - ApiDebugger

    IntelliJ IDEA插件 - ApiDebuggerApiDebugger,是一个开源的接口调试IntelliJ IDEA插件,具有与IDEA一致的界面,无需切换程序即可完成网络API请求,让你 ...

  5. Redis持久化概念

    redis持久化概念 Author:SimpleWu GitHub-redis 什么是持久化? 概念:把内存的数据保存在磁盘的过程. Redis的持久化? redis是内存数据库,它把数据存储在内存中 ...

  6. LeetCode(123):买卖股票的最佳时机 III

    Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必 ...

  7. Python计算器实操

    要求: 开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...

  8. Django标签&迭代&循环&过滤

    1.{% for Person in persons %}模板标签的替换,就是利用了基础模板的底层设计,嵌套了其他显示的内容.常见的内容替换标签{% block content %}{%endbloc ...

  9. cf1084d 非常巧妙的树形dp

    /* 给定n城市,m条道路,每条路耗油w,每个点有油a[i],从任意点出发,求最大可以剩下的油 dp[i]表示从i开始往下走的最大收益,ans表示最大结果 因为走过的路不能走,所以可以想到最优解肯定经 ...

  10. uva11827 处理下输入

    /*0.012s*/ #include<cstdio> #include<algorithm> using namespace std; ], n; int gcd(int a ...