新聞中心
這篇文章給大家分享的是有關(guān)Hive有多少種存儲格式的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
江口ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
hive文件存儲格式
1.textfile
textfile為默認(rèn)格式
存儲方式:行存儲
磁盤開銷大 數(shù)據(jù)解析開銷大
壓縮的text文件 hive無法進(jìn)行合并和拆分
2.sequencefile
二進(jìn)制文件,以
存儲方式:行存儲
可分割 壓縮
一般選擇block壓縮
優(yōu)勢是文件和hadoop api中的mapfile是相互兼容的。
3.rcfile
存儲方式:數(shù)據(jù)按行分塊 每塊按照列存儲
壓縮快 快速列存取
讀記錄盡量涉及到的block最少
讀取需要的列只需要讀取每個row group 的頭部定義。
讀取全量數(shù)據(jù)的操作 性能可能比sequencefile沒有明顯的優(yōu)勢
4.orc
存儲方式:數(shù)據(jù)按行分塊 每塊按照列存儲
壓縮快 快速列存取
效率比rcfile高,是rcfile的改良版本
5.自定義格式
用戶可以通過實現(xiàn)inputformat和 outputformat來自定義輸入輸出格式。
總結(jié):
textfile 存儲空間消耗比較大,并且壓縮的text 無法分割和合并 查詢的效率最低,可以直接存儲,加載數(shù)據(jù)的速度最高
sequencefile 存儲空間消耗最大,壓縮的文件可以分割和合并 查詢效率高,需要通過text文件轉(zhuǎn)化來加載
rcfile 存儲空間最小,查詢的效率最高 ,需要通過text文件轉(zhuǎn)化來加載,加載的速度最低
個人建議:text,seqfile能不用就盡量不要用 最好是選擇orc
Creating RCFile, SequenceFile, or Text Files
If you do not have an existing file to use, begin by creating one.
To create an RCFile, SequenceFile, or Text File table:
In the impala-shell interpreter, issue a command similar to:
create table rcfile_table (column_specs) stored as rcfile; create table sequencefile_table (column_specs) stored as sequencefile; create table textfile_table (column_specs) stored as textfile; -- If the STORED AS clause is omitted, the default is a comma-delimited TEXTFILE. create table default_table (column_specs);
Because Impala can query some kinds of tables that it cannot currently write to, after creating tables of certain file formats, you might use the Hive shell to load the data. See Understanding File Formats for details.
Enabling Compression for RCFile and SequenceFile Tables
You may want to enable compression on existing tables. Enabling compression provides performance gains in most cases and is supported for RCFile and SequenceFile tables. For example, to enable Snappy compression, you would specify the following additional settings when loading data through the Hive shell:
hive> SET hive.exec.compress.output=true; hive> SET mapred.max.split.size=256000000; hive> SET mapred.output.compression.type=BLOCK; hive> SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec; hive> insert overwrite table NEW_TABLE select * from OLD_TABLE;
If you are converting partitioned tables, you must complete additional steps. In such a case, specify additional settings similar to the following:
hive> create table NEW_TABLE (YOUR COLS) partitioned by (PARTITION COLS) stored as NEW_FORMAT; hive> SET hive.exec.dynamic.partition.mode=nonstrict; hive> SET hive.exec.dynamic.partition=true; hive> insert overwrite table NEW_TABLE partition(COMMA SEPARATED PARTITION COLS) select * from OLD_TABLE;
Remember that Hive does not require that you specify a source format for it. Consider the case of converting a table with two partition columns called "year" and "month" to a Snappy compressed SequenceFile. Combining the components outlined previously to complete this table conversion, you would specify settings similar to the following:
hive> create table TBL_SEQ (int_col int, string_col string) stored as SEQUENCEFILE; hive> SET hive.exec.compress.output=true; hive> SET mapred.max.split.size=256000000; hive> SET mapred.output.compression.type=BLOCK; hive> SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec; hive> SET hive.exec.dynamic.partition.mode=nonstrict; hive> SET hive.exec.dynamic.partition=true; hive> insert overwrite table TBL_SEQ select * from TBL;
To complete a similar process for a table that includes partitions, you would specify settings similar to the following:
hive> create table TBL_SEQ (int_col int, string_col string) partitioned by (year int) stored as SEQUENCEFILE; hive> SET hive.exec.compress.output=true; hive> SET mapred.max.split.size=256000000; hive> SET mapred.output.compression.type=BLOCK; hive> SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec; hive> SET hive.exec.dynamic.partition.mode=nonstrict; hive> SET hive.exec.dynamic.partition=true; hive> insert overwrite table TBL_SEQ partition(year) select * from TBL;
Note:
The compression type is specified in the following phrase:
SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
You could elect to specify alternative codecs such as GzipCodec here.
Hive的三種文件格式:TEXTFILE、SEQUENCEFILE、RCFILE中,TEXTFILE和SEQUENCEFILE的存儲格式都是基于行存儲的,RCFILE是基于行列混合的思想,先按行把數(shù)據(jù)劃分成N個row group,在row group中對每個列分別進(jìn)行存儲。另:Hive能支持自定義格式,詳情見:Hive文件存儲格式
基于HDFS的行存儲具備快速數(shù)據(jù)加載和動態(tài)負(fù)載的高適應(yīng)能力,因為行存儲保證了相同記錄的所有域都在同一個集群節(jié)點。但是它不太滿足快速的查詢響應(yīng)時間的要求,因為當(dāng)查詢僅僅針對所有列中的 少數(shù)幾列時,它就不能跳過不需要的列,直接定位到所需列;同時在存儲空間利用上,它也存在一些瓶頸,由于數(shù)據(jù)表中包含不同類型,不同數(shù)據(jù)值的列,行存儲不 易獲得一個較高的壓縮比。RCFILE是基于SEQUENCEFILE實現(xiàn)的列存儲格式。除了滿足快速數(shù)據(jù)加載和動態(tài)負(fù)載高適應(yīng)的需求外,也解決了SEQUENCEFILE的一些瓶頸。
下面對這幾種幾個作一個簡單的介紹:
TextFile:
Hive默認(rèn)格式,數(shù)據(jù)不做壓縮,磁盤開銷大,數(shù)據(jù)解析開銷大。
可結(jié)合Gzip、Bzip2、Snappy等使用(系統(tǒng)自動檢查,執(zhí)行查詢時自動解壓),但使用這種方式,hive不會對數(shù)據(jù)進(jìn)行切分,從而無法對數(shù)據(jù)進(jìn)行并行操作。
SequenceFile:
SequenceFile是Hadoop API 提供的一種二進(jìn)制文件,它將數(shù)據(jù)以
SequenceFile的文件結(jié)構(gòu)圖:
Header通用頭文件格式:
SEQ | 3BYTE |
Nun | 1byte數(shù)字 |
keyClassName | |
ValueClassName | |
compression | (boolean)指明了在文件中是否啟用壓縮 |
blockCompression | (boolean,指明是否是block壓縮) |
compression | codec |
Metadata | 文件元數(shù)據(jù) |
Sync | 頭文件結(jié)束標(biāo)志 |
Block-Compressed SequenceFile格式
基于Hadoop系統(tǒng)行存儲結(jié)構(gòu)的優(yōu)點在于快速數(shù)據(jù)加載和動態(tài)負(fù)載的高適應(yīng)能力,這是因為行存儲保證了相同記錄的所有域都在同一個集群節(jié)點,即同一個 HDFS塊。不過,行存儲的缺點也是顯而易見的,例如它不能支持快速查詢處理,因為當(dāng)查詢僅僅針對多列表中的少數(shù)幾列時,它不能跳過不必要的列讀??;此 外,由于混合著不同數(shù)據(jù)值的列,行存儲不易獲得一個極高的壓縮比,即空間利用率不易大幅提高。
列存儲
HDFS塊內(nèi)列存儲的例子
在HDFS上按照列組存儲表格的例子。在這個例子中,列A和列B存儲在同一列組,而列C和列D分別存儲在單獨的列組。查詢時列存儲能夠避免讀不必要的列, 并且壓縮一個列中的相似數(shù)據(jù)能夠達(dá)到較高的壓縮比。然而,由于元組重構(gòu)的較高開銷,它并不能提供基于Hadoop系統(tǒng)的快速查詢處理。列存儲不能保證同一 記錄的所有域都存儲在同一集群節(jié)點,行存儲的例子中,記錄的4個域存儲在位于不同節(jié)點的3個HDFS塊中。因此,記錄的重構(gòu)將導(dǎo)致通過集群節(jié)點網(wǎng)絡(luò)的大 量數(shù)據(jù)傳輸。盡管預(yù)先分組后,多個列在一起能夠減少開銷,但是對于高度動態(tài)的負(fù)載模式,它并不具備很好的適應(yīng)性。
RCFile結(jié)合行存儲查詢的快速和列存儲節(jié)省空間的特點:首先,RCFile保證同一行的數(shù)據(jù)位于同一節(jié)點,因此元組重構(gòu)的開銷很低;其次,像列存儲一樣,RCFile能夠利用列維度的數(shù)據(jù)壓縮,并且能跳過不必要的列讀取。
HDFS塊內(nèi)RCFile方式存儲的例子:
數(shù)據(jù)測試
源表數(shù)據(jù)記錄數(shù):67236221
第一步:創(chuàng)建三種文件類型的表,建表語法參考Hive文件存儲格式
Sql代碼
--TextFile
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 hzr_test_text_table PARTITION(product='xxx',dt='2013-04-22')
SELECT xxx,xxx.... FROM xxxtable WHERE product='xxx' AND dt='2013-04-22';
--SquenceFile
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;
set io.seqfile.compression.type=BLOCK;
INSERT OVERWRITE table hzr_test_sequence_table PARTITION(product='xxx',dt='2013-04-22')
SELECT xxx,xxx.... FROM xxxtable WHERE product='xxx' AND dt='2013-04-22';
--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 hzr_test_rcfile_table PARTITION(product='xxx',dt='2013-04-22')
SELECT xxx,xxx.... FROM xxxtable WHERE product='xxx' AND dt='2013-04-22';
第二步:測試insert overwrite table tablename select.... 耗時,存儲空間
類型 | insert耗時(S) | 存儲空間(G) |
Sequence | 97.291 | 7.13G |
RCFile | 120.901 | 5.73G |
TextFile | 290.517 | 6.80G |
insert耗時、count(1)耗時比較:
第三步:查詢響應(yīng)時間
測試一
Sql代碼
方案一,測試整行記錄的查詢效率:
select * from hzr_test_sequence_table where game='XXX' ;
select * from hzr_test_rcfile_table where game='XXX' ;
select * from hzr_test_text_table where game='XXX' ;
方案二,測試特定列的查詢效率:
select game,game_server from hzr_test_sequence_table where game ='XXX';
select game,game_server from hzr_test_rcfile_table where game ='XXX';
select game,game_server from hzr_test_text_table where game ='XXX';
文件格式 | 查詢整行記錄耗時(S) | 查詢特定列記錄耗時(S) |
sequence | 42.241 | 39.918 |
rcfile | 37.395 | 36.248 |
text | 43.164 | 41.632 |
方案耗時對比:
測試二:
本測試目的是驗證RCFILE的數(shù)據(jù)讀取方式和Lazy解壓方式是否有性能優(yōu)勢。數(shù)據(jù)讀取方式只讀取元數(shù)據(jù)和相關(guān)的列,節(jié)省IO;Lazy解壓方式只解壓相關(guān)的列數(shù)據(jù),對不滿足where條件的查詢數(shù)據(jù)不進(jìn)行解壓,IO和效率都有優(yōu)勢。
方案一:
記錄數(shù):698020
Sql代碼
insert overwrite local directory 'XXX/XXXX' select game,game_server from hzr_test_xxx_table where game ='XXX';
方案二:
記錄數(shù):67236221
Sql代碼
insert overwrite local directory 'xxx/xxxx' select game,game_server from hzr_test_xxx_table;
方案三:
記錄數(shù):
Sql代碼
insert overwrite local directory 'xxx/xxx'
select game from hzr_xxx_rcfile_table;
文件類型 | 方案一 | 方案二 | 方案三 |
TextFile | 54.895 | 69.428 | 167.667 |
SequenceFile | 137.096 | 77.03 | 123.667 |
RCFile | 44.28 | 57.037 | 89.9 |
上圖表現(xiàn)反應(yīng)在大小數(shù)據(jù)集上,RCFILE的查詢效率高于SEQUENCEFILE,在特定字段數(shù)據(jù)讀取時,RCFILE的查詢效率依然優(yōu)于SEQUENCEFILE。
感謝各位的閱讀!關(guān)于“Hive有多少種存儲格式”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享文章:Hive有多少種存儲格式
新聞來源:http://fisionsoft.com.cn/article/pdchpj.html