hive表存儲格式及壓縮

1.textfile

  • Hive數據表的默認格式,磁盤開銷大,數據解析開銷大
  • 存儲方式:行存儲
  • 壓縮方式:使用Gzip,Bzip2等壓縮算法壓縮,壓縮後的文件不支持split
  • 但在反序列化過程當中,必須逐個字符判斷是否是分隔符和行結束符,所以反序列化開銷會比SequenceFile高几十倍。
--建立數據表:
create table if not exists textfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as textfile;
--插入數據:
set hive.exec.compress.output=true; --啓用壓縮格式 
set mapred.output.compress=true;    
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  --指定輸出的壓縮格式爲Gzip  
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;      
insert overwrite table textfile_table select * from T_Name;

2.sequencefile

  • Hadoop API提供的一種二進制文件,以<key,value>的形式序列化到文件中
  • 存儲方式:行存儲
  • 壓縮方式:NONE,RECORD,BLOCK。Record壓縮率低,通常建議使用BLOCK壓縮
  • 優點是文件和hadoop api中的MapFile是相互兼容的
create table if not exists seqfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as sequencefile;
--插入數據操做:
set hive.exec.compress.output=true;  --啓用輸出壓縮格式
set mapred.output.compress=true;  
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  --指定輸出壓縮格式爲Gzip
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  
SET mapred.output.compression.type=BLOCK; --指定爲Block
insert overwrite table seqfile_table select * from T_Name;


3.rcfile

存儲方式:數據按行分組,每組內按列存儲,默認行組大小是4MB 算法

行列混合存儲的優勢:sql

  • 同一行的數據位於同一節點,所以元組重構的開銷很低
  • 與列存同樣,RCFile 可以利用列維度的數據壓縮,並跳過沒必要要的列讀取

  RCFile的一個行組包括三個部分:apache

  1.  第一部分是行組頭部的【同步標識】,主要用於分隔 hdfs 塊中的兩個連續行組
  2.  第二部分是行組的【元數據頭部】,用於存儲行組單元的信息,包括行組中的記錄數、每一個列的字節數、列中每一個域的字節數
  3.  第三部分是【表格數據段】,即實際的列存儲數據。在該部分中,同一列的全部域順序存儲。
     從圖能夠看出,首先存儲了列 A 的全部域,而後存儲列 B 的全部域等

create table if not exists rcfile_table(
site string,
url  string,
pv   bigint,
label string)
row format delimited fields terminated by '\t'
stored as rcfile;
--插入數據操做:
set hive.exec.compress.output=true;  
set mapred.output.compress=true;  
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;  
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;  
insert overwrite table rcfile_table select * from T_Name;

4.orcfile

  • 存儲方式:數據按行分組,每塊按照列存儲,默認行組大小爲250MB
  • 壓縮方式:ZLIB和SNAPPY,默認ZLIB。壓縮速度快 快速列存取
  • 效率比rcfile高,是rcfile的改良版本

運用ORC File能夠提升Hive的讀、寫以及處理數據的性能。
與RCFile格式相比,ORCFile有如下優勢:
  (1)、每一個task只輸出單個文件,這樣能夠減小NameNode的負載
  (2)、支持複雜的數據類型,如: datetime, decimal, 以及一些複雜類型(struct, list, map, and union)
  (3)、在文件中存儲了一些輕量級的索引數據
  (4)、基於數據類型的塊模式壓縮:integer類型列用行程長度編碼;String類型列用字典編碼;
  (5)、用多個互相獨立的RecordReaders並行讀相同的文件
  (6)、無需掃描markers就能夠分割文件
  (7)、綁定讀寫所須要的內存
  (8)、metadata的存儲是用 Protocol Buffers的,因此它支持添加和刪除一些列api

create table Addresses (
  name string,
  street string,
  city string
) 
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="NONE");

5.自定義格式

  • 用戶能夠經過實現inputformat和 outputformat來自定義輸入輸出格式。
create table myfile_table(str STRING)  
stored as  
inputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'  
outputformat 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextOutputFormat';

查詢測試

    實驗所用表爲15列,共約300多條非重複記錄,總記錄數20123648oop

格式 大小 可分割 * col5 count(*) max(c1) max(c1) where c5='xxx'
textfile 1.3GB 31s 21s 23s 25s 26s
rc-none 1.0GB 31s 20s 22s 25s 23s
rc-gzip 1.6MB 27s 1s 1s 31s 28s
orc-none 53.3MB 20s 1s 1s 21s 22s
orc-zlib 645.2KB 20s 1s 1s 21s 21s

    實驗所用表爲72列,皆爲非重複記錄,總記錄數爲7644851性能

格式 大小 可分割  *        c5       count(*) max(c5)

max(c5) where c9='T'測試

c2,count(*) group by c2編碼

textfile 3.3G 27 10 12 12 7 28
seq-none 3.46G 25 7 12 12 16 8
seq-block 0.91G 47 16 10 10 10 10
orc-none 1.7G 37 5 5 5 5 8
orc-zlib 0.66G 37 6 8 5 5 8

    數據倉庫的特色:一次寫入、屢次讀取、並行執行,所以,總體來看,ORCFile相比其餘格式具備較明顯的優點。url

  • TextFile 默認格式加載速度最快,可採用Gzip、bzip2等壓縮,壓縮後的沒法split,沒法並行處理
  • SequenceFile 壓縮率最低,查詢速度通常三種壓縮格式NONE,RECORD,BLOCK
  • RCfile 壓縮率高,查詢速度最快,數據加載最慢
  • ORCfile 壓縮率高,查詢速度快,數據加載快
相關文章
相關標籤/搜索