Hive文件存儲格式包括以下幾類:
1、TEXTFILE
2、SEQUENCEFILE
3、RCFILE
4、ORCFILE
其中TEXTFILE為默認格式,建表時不指定默認為這個格式,導入數據時會直接把數據文件拷貝到hdfs上不進行處理。
SEQUENCEFILE,RCFILE,ORCFILE格式的表不能直接從本地文件導入數據,數據要先導入到textfile格式的表中, 然后再從表中用insert導入SequenceFile,RCFile,ORCFile表中。
?
更多用法,一定要去看官網啊!!!?
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL
?
?
一、TEXTFILE 格式
默認格式,數據不做壓縮,磁盤開銷大,數據解析開銷大。 可結合Gzip、Bzip2使用(系統自動檢查,執行查詢時自動解壓),但使用這種方式,Hive不會對數據進行切分, 從而無法對數據進行并行操作。
示例:
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;
?
插入數據操作:
Hive> Hive.exec.compress.output=true; Hive> set mapred.output.compress=true; Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; Hive> insert overwrite table textfile_table select * from textfile_table;
?
?
?
二、SEQUENCEFILE 格式
SequenceFile是Hadoop API提供的一種二進制文件支持,其具有使用方便、可分割、可壓縮的特點。?
SequenceFile支持三種壓縮選擇:NONE,RECORD,BLOCK。Record壓縮率低,一般建議使用BLOCK壓縮。
示例:
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;
?
插入數據操作:
Hive> set Hive.exec.compress.output=true; Hive> set mapred.output.compress=true; Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; Hive> SET mapred.output.compression.type=BLOCK; Hive> insert overwrite table seqfile_table select * from textfile_table;
?
?
?
三、RCFILE 文件格式
RCFILE是一種行列存儲相結合的存儲方式。首先,其將數據按行分塊,保證同一個record在一個塊上,避免讀一個記錄需要讀取多個block。
其次,塊數據列式存儲,有利于數據壓縮和快速的列存取。
RCFILE文件示例:
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;
?
插入數據操作:
Hive> set Hive.exec.compress.output=true; Hive> set mapred.output.compress=true; Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; Hive> insert overwrite table rcfile_table select * from textfile_table;
?
?
?
四、ORCFILE()
以后補充
?
?
五、再看TEXTFILE、SEQUENCEFILE、RCFILE三種文件的存儲情況:
[hadoop@master ~]$ hadoop dfs -dus /user/Hive/warehouse/*
hdfs://master?:9000/user/Hive/warehouse/hbase_table_1 0
hdfs://master?:9000/user/Hive/warehouse/hbase_table_2 0
hdfs://master?:9000/user/Hive/warehouse/orcfile_table 0
hdfs://master?:9000/user/Hive/warehouse/rcfile_table 102638073
hdfs://master?:9000/user/Hive/warehouse/seqfile_table 112497695
hdfs://master?:9000/user/Hive/warehouse/testfile_table 536799616
hdfs://master?:9000/user/Hive/warehouse/textfile_table 107308067
[hadoop@singlehadoop ~]$ hadoop dfs -ls /user/Hive/warehouse/*/
-rw-r--r-- 2 hadoop supergroup 51328177 2014-03-20 00:42 /user/Hive/warehouse/rcfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 51309896 2014-03-20 00:43 /user/Hive/warehouse/rcfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 56263711 2014-03-20 01:20 /user/Hive/warehouse/seqfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 56233984 2014-03-20 01:21 /user/Hive/warehouse/seqfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 536799616 2014-03-19 23:15 /user/Hive/warehouse/testfile_table/weibo.txt
-rw-r--r-- 2 hadoop supergroup 53659758 2014-03-19 23:24 /user/Hive/warehouse/textfile_table/000000_0.gz
-rw-r--r-- 2 hadoop supergroup 53648309 2014-03-19 23:26 /user/Hive/warehouse/textfile_table/000001_1.gz
總結: 相比TEXTFILE和SEQUENCEFILE,RCFILE由于列式存儲方式,數據加載時性能消耗較大,但是具有較好的壓縮比和查詢響應。
數據倉庫的特點是一次寫入、多次讀取,因此,整體來看,RCFILE相比其余兩種格式具有較明顯的優勢。
?
?
?
以下,本文轉自于。http://blog.csdn.net/cnbird2008/article/details/9182869
Hive數據壓縮
本文介紹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<TEXTFILE|SEQUENCEFILE> to override</description>
</property>
<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、對輸出文本進行處理時不壓縮
?
本文轉自大數據躺過的坑博客園博客,原文鏈接:http://www.cnblogs.com/zlslch/p/6103760.html,如需轉載請自行聯系原作者