我们在使用Hive的时候肯定遇到过建立了一张分区表,然后手动(比如使用 cp 或者 mv )将分区数据拷贝到刚刚新建的表作为数据初始化的手段;但是对于分区表我们需要在hive里面手动将刚刚初始化的数据分区加入到hive里面,这样才能供我们查询使用,我们一般会想到使用 alter table add partition 命令手动添加分区,但是如果初始化的分区太多,这样一条一条地手动添加分区不免过于麻烦(虽然我们可以写个脚本生成添加分区的命令)。今天我将给大家介绍的命令可以只使用一次即可添加全部的分区,如下:

MSCK REPAIR TABLE table_name;

运行上面的命令后,Hive会检测HDFS目录下存在但表的metastore中不存在的partition元信息,然后更新到metastore中。官方文档对其的描述如下:
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.

下面我来介绍如何使用这个命令,我新建了名为 temp.iteblog_hadoop 的表,然后手动 mv 了几个目录的数据到这个表数据目录下,这几个手动添加的目录在hive中目前肯定无法被查询到,所以我可以使用下面的命令来识别这些分区:

hive> show partitions temp.iteblog_hadoop;
OK
Time taken: 1.491 seconds
hive> MSCK REPAIR TABLE temp.iteblog_hadoop;
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 11595
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 23861
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 784358
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 2396891
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 2234631
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 36621058
17/02/21 16:07:15 WARN log: Updating partition stats fast for: iteblog_hadoop
17/02/21 16:07:15 WARN log: Updated size to 21093250
OK
Partitions not in metastore:    iteblog_hadoop:dt=2017-02-15    iteblog_hadoop:dt=2017-02-16    iteblog_hadoop:dt=2017-02-17    iteblog_hadoop:dt=2017-02-18    iteblog_hadoop:dt=2017-02-19    iteblog_hadoop:dt=2017-02-20    iteblog_hadoop:dt=2017-02-21
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-15
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-16
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-17
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-18
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-19
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-20
Repair: Added partition to metastore temp.iteblog_hadoop:dt=2017-02-21
Time taken: 0.575 seconds, Fetched 8 row(s)

从上面的命令可以看出,已经将所有的分区加入到Hive的metastore了,是不是很方便啊。

为了让 MSCK 命令工作,分区的目录名必须是 /partition_name=partition_value/结构的,否则将无法添加分区。这时候你必须使用add partition命令了。

Hive分区修复命令MSCK介绍与使用相关推荐

  1. Hive有分区文件到时select不到数据问题-----修复分区命令 msck repair table xxxxx

    问题:在导数据到hive分区表时, 手动把HDFS路径建好了,然后把对应的文件添加到路径下. 这时用select语句查询却查不到数据. 原因:虽然分区文件有了,但是分区信息没有添加到hive元数据表中 ...

  2. HIVE——常用sql命令总结

    文章目录 hive常用交互命令 `-e`执行sql `-f`执行脚本中sql语句 hive cli命令行窗口操作hdfs 查看hive中输入的所有历史命令 库 创建库 查看库 使用库 修改库 删除库 ...

  3. 250g硬盘linux分区,linux分区格式化命令使用方法

    linux分区格式化命令使用方法 首先使用root权限登陆进入linux操作界面 使用fdisk -l 参数查看设备名跟设备状态 类似如下 : [root@ecofe2 /]# fdisk -l Di ...

  4. 磁盘修复命令CHKDSK回顾

    磁盘修复命令CHKDSK回顾 Check Disk,简称"CHKDSK",是一种Windows内置的实用程序,可以检查硬盘文件系统的完整性,并可以修复FAT16.FAT32和NTF ...

  5. Hive分区——Partition(静态分区,动态分区)详解

    文章目录 分区目的 分区的创建 1.创建建表时候不管动态还是静态分区 2.执行load 装载数据 3.查看表结构,查询分区表的数据 4.静态分区: 5.动态分区: 多分区 表分区的增删改查 分区目的 ...

  6. linux错误关机修复工具,Linux_Linux文件系统损坏后的修复技巧详细介绍, Linux不正常关机,有时候再 - phpStudy...

    Linux文件系统损坏后的修复技巧详细介绍 Linux不正常关机,有时候再次启动时会报文件系统损坏,如何修复文件? 首先会让你输入root用户的密码. 1)出错的时候如果告诉你是哪一块硬盘的分区有问题 ...

  7. Hive分区、分桶操作及其比较(转自:http://blog.csdn.net/epitomizelu/article/details/41911657)

    1,Hive分区. 是指按照数据表的某列或某些列分为多个区,区从形式上可以理解为文件夹,比如我们要收集某个大型网站的日志数据,一个网站每天的日志数据存在同一张表上,由于每天会生成大量的日志,导致数据表 ...

  8. HDFS命令行客户端使用,命令行客户端支持的命令参数,常用命令参数介绍

    3.HDFS的shell(命令行客户端)操作 3.1 HDFS命令行客户端使用 HDFS提供shell命令行客户端,使用方法如下: [toto@hadoop hadoop-2.8.0]$ hdfs d ...

  9. linux磁盘分区fdisk命令详解

    1.什么是分区?     分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用.分区表是一个硬盘分区的索引,分区的信息都会写进分区表. 2.为什么要有多个分区? 防 ...

最新文章

  1. easyui datagrid checkbox的相关属性整理
  2. oracle 存储过程定义及调试,并终于被C# 调用 代码
  3. 技术正文 history命令添加时间---测试磁盘写入速度
  4. 在linuxc中如何实现输入隐藏
  5. LeetCode 897 递增顺序搜索树
  6. python中event_Python Event实现线程通信
  7. ?请问怎样将excel表格中的时间格式读取为字符串格式?
  8. SAP License:集团化企业的协同管理
  9. NumPy中特别的数组函数 By Youki~
  10. 不需要的系统垃圾把它杀掉!
  11. “ 鸡尾酒会问题”(cocktail party problem)
  12. DataBinding 的集成:点击AIL+Enter , 没有出现【convert to data binding layout】怎么办?
  13. 微信点拼多多砍价显示服务器吃撑,微信狠起来自己人都打?最严外链规范实行,微视、拼多多砍价链接被限制!...
  14. 计讯物联山海步道安全管理与运维解决方案
  15. 在计算机软件中怎么拍照,计算机相机相机软件,这三个软件不仅用于拍照
  16. 用ios企业证书发布ipa到服务器上扫码下载
  17. 基于CCE Kubernetes网络与持久化存储实战
  18. 鏖战5小时,44对1,扎克伯格被参议员怒怼,结果他坐着就赚了28亿美元
  19. 详细讲解一下Linux内核系统结构(图例解析)
  20. 上网必备 11款流氓软件清除工具推荐

热门文章

  1. python控制结束多线程_python进阶八——并发编程之多线程
  2. Chrome Extension 介绍
  3. 运用matlab求解梁的变形例题,matlab与结构力学
  4. 超微服务器做系统,超微服务器做系统
  5. 【Latex】在图片标题中加入脚注
  6. 100 道 Linux 常见面试题,建议收藏!
  7. uboot-顶层Makefile注释
  8. c语言程序与化学有关的程序,化工热力学C语言程序.doc
  9. “/Ox”和“/RTC1”命令行选项不兼容 或者 ml.exe 退出
  10. 这是关于如何学好算法