最近在使用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. Mudo C++网络库第二章学习笔记

    线程同步的精要 并发有两种基本的模型: 一种是message passing(消息传递); 另一种是shared memory(共享内存); 在分布式系统中(有多台物理机需要通信), 运行在多台机器上 ...

  2. var_export 掉咋天

    var_export     文件缓存经常使用    输出或返回一个变量的字符串表示 /** * 写入缓存 * * @param string $id * @param mixed $data * @ ...

  3. 转载:UML学习(四)-----状态图(silent)

    原文:http://www.cnblogs.com/silent2012/archive/2011/11/01/2178278.html 状态图主要用于描述对象具有的各种状态.状态之间的转换过程以及触 ...

  4. 前端 -----函数和伪数组(arguments)

    函数   函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. 简化编程,让编程模块化. ...

  5. 开通博客的第一天上传我的C#基础笔记。

    1.索引器  string arrStr = "sddfdfgfh";  索引器的目的就是为了方便而已,可以在该类型的对象后面直接写[]访问该对象里面的成员  Console.Wr ...

  6. IntelliJ IDEA插件 - ApiDebugger

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

  7. destoon使用

    使用小计 1.判断是否是手机端 {$DT_TOUCH}模板中使用 2.判断句 {if}  {/if} 3.表单管理 扩展功能-----表单管理:添加表单---->管理表单选项------> ...

  8. OCM 学习练习题目

    1:数据安装操作练习:考试题目 1: Creating a database & Server Configuration --[101]-- #创建数据库 1. Create the dat ...

  9. SpringCloud路由(网关)

    springcloud网关接口就类似于转发 搭建路由网关项目(ZuulDemo) 1.创建pom.xml <project xmlns="http://maven.apache.org ...

  10. xampp 安装以及相关问题

    1.安装xampp   说明:xampp集成了mysql,Apache,php,360软件里面就有 2.mysql端口被占用.              如果电脑上已安装MySql数据库,还想用XAM ...