Hive:ORC File Format存儲格式詳解


一、定義

  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文件結構:

三、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字段里面出現,他們是:

Key Default Notes
orc.compress ZLIB high level compression (one of NONE, ZLIB, SNAPPY)
orc.compress.size 262,144 number of bytes in each compression chunk
orc.stripe.size 268435456 number of bytes in each stripe
orc.row.index.stride 10,000 number of rows between index entries (must be >= 1000)
orc.create.index true whether 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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM