本文介紹Hadoop系統中Hive資料壓縮方案的比較結果及具體壓縮方法。
關于Hadoop HDFS檔案的壓縮格式選擇,我們通過多個真實的Track資料做測試,得出結論如下:
1. 系統的預設壓縮編碼方式 DefaultCodec 無論在壓縮性能上還是壓縮比上,都優于GZIP 壓縮編碼。這一點與網上的一些觀點不大一緻,網上不少人認為GZIP的壓縮比要高一些,估計和Cloudera的封裝及我們Track的資料類型有關。
2. Hive檔案的RCFile 的在壓縮比,壓縮效率,及查詢效率上都優于SEQENCE FILE (包括RECORD, BLOCK 級别) 。
3. 所有壓縮檔案均可以正常解壓為TEXT 檔案,但比原始檔案略大,可能是行列重組造成的。
關于壓縮檔案對于其他元件是适用性如下:
1. Pig 不支援任何形式的壓縮檔案。
2. Impala 目前支援SequenceFile的壓縮格式,但還不支援RCFile的壓縮格式。
綜上所述:
從壓縮及查詢的空間和時間性能上來說,DefaultCodeC + RCFile的壓縮方式均為最優,但使用該方式,會使得Pig 和Impala 無法使用(Impala的不相容不确定是否是暫時的)。
而DefaultCodeC+ SequenceFile 在壓縮比,查詢性能上略差于RCFile (壓縮比約 6:5), 但可以支援 Impala實時查詢。
推薦方案:
采用RCFile 方式壓縮曆史資料。FackBook全部hive表都用RCFile存資料。
隻需要兩步:
1. 建立表時指定壓縮方式,預設不壓縮,以下為示例:
create external table track_hist(
id bigint, url string, referer string, keyword string, type int, gu_idstring,
…/*此處省略中間部分字段*/ …, string,ext_field10 string)
partitioned by (ds string) stored as RCFile location '/data/share/track_histk' ;
2. 插入資料是設定立即壓縮
SET hive.exec.compress.output=true;
insert overwrite table track_histpartition(ds='2013-01-01')
select id,url, …/*此處省略中間部分字段*/ …, ext_field10 fromtrackinfo
where ds='2013-01-01';
在hive-site.xml中設定:
<property>
<name>hive.default.fileformat</name>
<value>RCFile</value>
<description>Default file format for CREATE TABLE statement.Options are TextFile and SequenceFile. Users can explicitly say CREAT
E TABLE ... STORED AS&lt;TEXTFILE|SEQUENCEFILE&gt; to override</description>
</property>
<name>hive.exec.compress.output</name>
<value>true</value>
<description> This controls whether the final outputs of a query(to a local/hdfs file or a hive table) is compressed. The compres
sion codec and other options are determinedfrom hadoop config variables mapred.output.compress* </description>
1、Map階段輸出不進行壓縮
2、對輸出文本進行處理時不壓縮