Hive:ORC File Format存储格式详解

摘要:
这种文件格式可以提供存储Hive数据的有效方式。(3) . 一些轻量级索引数据存储在文件中;a、 整型列是游程长度编码;2、 ORCFile文件结构ORCFile包含一组行数据,主要用于存储压缩参数和压缩页脚的大小。4、 如何使用ORCFile在Hive中创建Hive表?我们应该指定文件存储格式。
一、定义

  ORC File,它的全名是Optimized Row Columnar (ORC) file,其实就是对RCFile做了一些优化。

据官方文档介绍,这种文件格式可以提供一种高效的方法来存储Hive数据。它的设计目标是来克服Hive其他格式的缺陷。

运用ORC File可以提高Hive的读、写以及处理数据的性能。
和RCFile格式相比,ORC File格式有以下优点:
  (1)、每个task只输出单个文件,这样可以减少NameNode的负载;
  (2)、支持各种复杂的数据类型,比如: datetime, decimal, 以及一些复杂类型(struct, list, map, and union);
  (3)、在文件中存储了一些轻量级的索引数据;
  (4)、基于数据类型的块模式压缩:a、integer类型的列用行程长度编码(run-length encoding);b、String类型的列用字典编码(dictionary encoding);
  (5)、用多个互相独立的RecordReaders并行读相同的文件;
  (6)、无需扫描markers就可以分割文件;
  (7)、绑定读写所需要的内存;
  (8)、metadata的存储是用 Protocol Buffers的,所以它支持添加和删除一些列。

二、ORC File文件结构

  ORC File包含一组组的行数据,称为stripes,除此之外,ORC File的file footer还包含一些额外的辅助信息。

       在ORC File文件的最后,有一个被称为postscript的区,它主要是用来存储压缩参数及压缩页脚的大小。
        在默认情况下,一个stripe的大小为250MB。大尺寸的stripes使得从HDFS读数据更高效。
  在file footer里面包含了该ORC File文件中stripes的信息,每个stripe中有多少行,以及每列的数据类型。

当然,它里面还包含了列级别的一些聚合的结果,比如:count, min, max, and sum。

下图显示出可ORC File文件结构:

Hive:ORC File Format存储格式详解第1张

三、Stripe结构

  从上图我们可以看出,每个Stripe都包含index data、row data以及stripe footer。Stripe footer包含流位置的目录;Row data在表扫描的时候会用到。
  Index data包含每列的最大和最小值以及每列所在的行。

  行索引里面提供了偏移量,它可以跳到正确的压缩块位置。具有相对频繁的行索引,使得在stripe中快速读取的过程中可以跳过很多行,尽管这个stripe的大小很大。

       在默认情况下,最大可以跳过10000行。拥有通过过滤谓词而跳过大量的行的能力,你可以在表的 secondary keys 进行排序,从而可以大幅减少执行时间。

       比如你的表的主分区是交易日期,那么你可以对次分区(state、zip code以及last name)进行排序。

四、Hive里面如何用ORCFile

  在建Hive表的时候我们就应该指定文件的存储格式。所以你可以在Hive QL语句里面指定用ORCFile这种文件格式,如下:

CREATE TABLE ... STORED AS ORC
 
ALTER TABLE ... [PARTITION partition_spec] SET FILEFORMAT ORC
 
SET hive.default.fileformat=Orc

所有关于ORCFile的参数都是在Hive QL语句的TBLPROPERTIES字段里面出现,他们是:

KeyDefaultNotes
orc.compressZLIBhigh level compression (one of NONE, ZLIB, SNAPPY)
orc.compress.size262,144number of bytes in each compression chunk
orc.stripe.size268435456number of bytes in each stripe
orc.row.index.stride10,000number of rows between index entries (must be >= 1000)
orc.create.indextruewhether to create row indexes

下面的例子是建立一个没有启用压缩的ORCFile的表

create table Addresses (
  name string,
  street string,
  city string,
  state string,
  zip int
) stored as orc tblproperties ("orc.compress"="NONE");
五、序列化和压缩

  对ORCFile文件中的列进行压缩是基于这列的数据类型是integer或者string。具体什么序列化我就不涉及了。。想深入了解的可以看看下面的英文:

Integer Column Serialization
Integer columns are serialized in two streams.
  1、present bit stream: is the value non-null?
  2、data stream: a stream of integers
Integer data is serialized in a way that takes advantage of the common distribution of numbers:
  1、Integers are encoded using a variable-width encoding that has fewer bytes for small integers.
  2、Repeated values are run-length encoded.
  3、Values that differ by a constant in the range (-128 to 127) are run-length encoded.
The variable-width encoding is based on Google's protocol buffers and uses the high bit to represent whether this byte is not the last and the lower 7 bits to encode data. To encode negative numbers, a zigzag encoding is used where 0, -1, 1, -2, and 2 map into 0, 1, 2, 3, 4, and 5 respectively.

Each set of numbers is encoded this way:
  1、If the first byte (b0) is negative:
    -b0 variable-length integers follow.
  2、If the first byte (b0) is positive:
    it represents b0 + 3 repeated integers
    the second byte (-128 to +127) is added between each repetition
    1 variable-length integer.
In run-length encoding, the first byte specifies run length and whether the values are literals or duplicates. Duplicates can step by -128 to +128. Run-length encoding uses protobuf style variable-length integers.

String Column Serialization

Serialization of string columns uses a dictionary to form unique column values The dictionary is sorted to speed up predicate filtering and improve compression ratios.

String columns are serialized in four streams.
  1、present bit stream: is the value non-null?
  2、dictionary data: the bytes for the strings
  3、dictionary length: the length of each entry
  4、row data: the row values
Both the dictionary length and the row values are run length encoded streams of integers.

免责声明:文章转载自《Hive:ORC File Format存储格式详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇数据加密之SymmetricAlgorithm加密VC下发布的Release版程序崩溃后的异常捕捉与查找下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

oracle读写文件--利用utl_file包对磁盘文件的读写操作

摘要: 用户提出一个需求,即ORACLE中的一个表存储了照片信息,字段类型为BLOB,要求能导出成文件形式. 本想写个C#程序来做,后来想起ORACLE有很多包,功能很好很强大,于是网上参考了些文章完成了. 主要是用了ORACLE的两个包:UTL_FILE和DBMS_LOB. 实现过程: 第一步:以管理员用户登陆设置可操作目录 --CREATE...

HTML5Audio/Video全解(疑难杂症)

1、mp4格式视频无法在chrome中播放   Chrome浏览器支持HTML5,它支持原生播放部分的MP4格式(不用通过Flash等插件)。为 什么是部分MP4呢?MP4有非常复杂的含义(见http://en.wikipedia.org/wiki/Mp4),普通人对MP4的理解是后缀 为.mp4的文件。但MP4本身不是一种简单的视频格式,它是一个包装了...

运行软件出现:模块“msvcp110.dll”已加载,但找不到入口点DllRegister

根据百度大多数回答来说 1:先是出现   无法启动程序,因为计算机丢失mfc110.dll 尝试重新安装该程序以解决问题   错误处理:下载或者在别人电脑上拷一份 如:'msvcp110.dll‘ 这类文件然后复制粘贴到系统盘即使/C/windows/system32或者64位系统就放到syswow64     楼主亲测是不能成功的。至少大多数情况是这样...

Python 2.75升级3.6.3

https://blog.csdn.net/wwwdaan5com/article/details/78218277 Centos 7 默认yum安装python 是2.7.5, (网上看了很多升级都是错的 导致python混乱,导致yum坏了,本人还重装了yum:http://www.cnblogs.com/eason-d/p/8608180.html)...

Ftp进行文件的上传和下载

一、开篇之前,先给大家推荐一款好用的FTP工具:FTP客户端—IIs7服务器管理工具             --官网地址:http://fwqglgj.iis7.net/cp/ftp/?cmc-zc作为FTP客户端,它支持批量管理ftp站点。定时上传和定时下载,定时备份,且操作简洁。同时iis7服务器管理工具还是vnc客户端。并且支持批量管理管理wind...

【CCS仿真】如何将CCS仿真时memory中的数据以Hex、Integer、 Long 、Float、 Addressable Unit类型保存到PC

2013-12-04 19:07:05 将在CCS中仿真的数据导入电脑上时,可以选择不同的数据类型,以便分析,具体方法如下: 在CCS菜单中,选择File—>Data—>Save,弹出以下窗口:                         在文件名中输入要保存的文件的名字,在保存类型中可以选择保存的文件类型以及格式。文件类型有dat文件与...