hive之存儲格式

今天給大家分享一個主題:Storage Format, 也就是存儲格式

我們先在 hive 里建張表,打開 hive 的控制臺,創(chuàng)建一個表
create table t(id int, name string);
你后面可以加 row format,我們通過 desc formatted,來看一下

往下看,這個地方有一個 Input、output
這個用來處理我們輸入數(shù)據(jù)的
這個用來處理輸出的

在這個地方,他所制定的是文本類型的

DDL
其實(shí)關(guān)鍵點(diǎn)就是這個地方,

你寫與不寫,默認(rèn)的都是 TextFile
學(xué)一個框架,會用只是最基本的

Storage Format 存儲格式
DDL指定語句:STORED AS file_format
file_format:
: SEQUENCEFILE
| TEXTFILE -- (Default, depending on hive.default.fileformat configuration)
| RCFILE -- (Note: Available in Hive 0.6.0 and later)
| ORC -- (Note: Available in Hive 0.11.0 and later)
| PARQUET -- (Note: Available in Hive 0.13.0 and later)
| AVRO -- (Note: Available in Hive 0.14.0 and later)
| JSONFILE -- (Note: Available in Hive 4.0.0 and later)
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname

通過STORED AS 關(guān)鍵字來設(shè)置Hive數(shù)據(jù)文件的存儲格式

行式存儲 vs 列式存儲
一條記錄有1000列/字段構(gòu)成
select a,b,c from t where d = ?
需要讀取非常多的不是必要的字段數(shù)據(jù) ==> 性能不太高的

列式存儲:不同的字段存儲在不同的block塊上,只select 幾個字段的時候性能是高的

場景:select * from t 
行式存儲可以,列式存儲具有性能的開銷,但select * 工作中使用不是太多
列式存儲用的是比較多的
    好處:字段單獨(dú)存在各自的block上面,需要哪些列你就去哪些列里面去拿,相同列的數(shù)據(jù)類型是一樣的,可以提升壓縮,進(jìn)而可以提升整體的一個性能

textfile
Hive default format
默認(rèn)數(shù)據(jù)是不壓縮,當(dāng)然你也可以使用你需要的壓縮格式對數(shù)據(jù)進(jìn)行壓縮

純文本 18.1M
純文本 + bzip2 3.6M
sequencefile:19.6M

sequencefile
個人不太建議使用的!
原因:
把數(shù)據(jù)以<k,v>的形式序列化到文件中,類似于二進(jìn)制格式

相對于text來說,多了很多信息,例如 header

注意:
    stored as sequencefile的hive表,不能以 load data 的方式加載數(shù)據(jù),
    要使用 insert into/overwrite table 的方式才可以

sequencefile:19.6M

create table page_views_seq(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as sequencefile;

insert into table page_views_seq select * from page_views;

[hadoop@spark000 ~]$ hadoop fs -du -s -h hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_seq
20.6 M 20.6 M hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_seq

=================>
RCFILE:0.6.0版本以后才有
facebook開源的面向列存儲的一個項(xiàng)目
Hive added the RCFile format in version 0.6.0

Row Group的概念,大小是比較小的,只有4M
行列混合的一種存儲模式
性能提升兩個角度
    存儲
    計(jì)算性能

create table page_views_rcfile(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as rcfile;

load data local inpath '/home/hadoop/data/page_views.dat' overwrite into table page_views_rcfile;

hive (ruozedata_hive)> load data local inpath '/home/hadoop/data/page_views.dat' overwrite into table page_views_rcfile;

Loading data to table ruozedata_hive.page_views_rcfile
Failed with exception Wrong file format. Please check the file's format.
Remote Exception: null

也只能使用 insert into/overwrite table 的方式

insert into table page_views_rcfile select * from page_views;

create table page_views_rcfile
row format delimited fields terminated by '\t'
stored as rcfile
as select * from page_views;

若老測試的是 17.9M
我測試的是 3.2M,這是使用 bzip2 壓縮后的

ORC:0.11.0版本以后有的
優(yōu)化后的列式存儲,也是Apache的頂級項(xiàng)目

支持ACID事務(wù)
內(nèi)置索引
復(fù)雜類型

使用ORC可以提升性能

stripe:250M
    Index Data
    Row Data
    Stripe Footer

ORC的參數(shù)
    orc.compress:可以使用壓縮,默認(rèn) ZLIB,high level compression (one of NONE, ZLIB, SNAPPY)

create table page_views_orc(
track_time string,
url string,
session_id string,
referer string,
ip string,
end_user_id string,
city_id string
) row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="NONE");

create table page_views_orc
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="NONE")
as select * from page_views;

不壓縮采用ORC存儲的數(shù)據(jù)大?。?.7M

create table page_views_orc_zlib
row format delimited fields terminated by '\t'
stored as orc tblproperties ("orc.compress"="ZLIB")
as select * from page_views;

壓縮采用ORC存儲的數(shù)據(jù)大?。?.8M

[hadoop@spark000 ~]hadoop fs -du -s -h hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_orc 7.7 M 7.7 M hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_orc [hadoop@spark000 ~] hadoop fs -du -s -h hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_orc_zlib
2.8 M 2.8 M hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views_orc_zlib

回去把文檔好好看一下
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ORC

PARQUET
twitter 和 cloudera 公司合并開源的

create table page_views_parquet
row format delimited fields terminated by '\t'
stored as parquet
as select * from page_views;

不壓縮采用parquet存儲的數(shù)據(jù)大小:13.1M

set parquet.compression=gzip;

create table page_views_parquet_gzip
row format delimited fields terminated by '\t'
stored as parquet
as select * from page_views;

gzip壓縮采用parquet存儲的數(shù)據(jù)大?。?.9M

存儲角度(未使用壓縮)
textfile:18.1M
sequencefile:19.6M
rcfile:17.9M
orc:7.7M
parquet:13.1M

計(jì)算性能
時間
讀取的數(shù)據(jù)量

[hadoop@spark000 ~]$ hadoop fs -du hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views
19014993 19014993 hdfs://spark000:8020/user/hive/warehouse/ruozedata_hive.db/page_views/page_views.dat
select count(1) from page_views where session_id = 'B58W48U4WKZCJ5D1T3Z9ZY88RU7QA7B1';
Cumulative CPU: 3.76 sec HDFS Read: 19024039

20501449
select count(1) from page_views_seq where session_id = 'B58W48U4WKZCJ5D1T3Z9ZY88RU7QA7B1';
Cumulative CPU: 4.06 sec HDFS Read: 20510558

18799578
select count(1) from page_views_rcfile where session_id = 'B58W48U4WKZCJ5D1T3Z9ZY88RU7QA7B1';
Cumulative CPU: 3.23 sec HDFS Read: 3726748

8101548
select count(1) from page_views_orc where session_id = 'B58W48U4WKZCJ5D1T3Z9ZY88RU7QA7B1';
Cumulative CPU: 3.39 sec HDFS Read: 1811458

13720967
select count(1) from page_views_parquet where session_id = 'B58W48U4WKZCJ5D1T3Z9ZY88RU7QA7B1';
Cumulative CPU: 3.39 sec HDFS Read: 2688389

好好的看看這篇文章
https://blog.cloudera.com/orcfile-in-hdp-2-better-compression-better-performance/

higher compression ==> 存儲
better query performance ==> 計(jì)算性能

hive.optimize.ppd=true


RCFile
facebook

內(nèi)置索引
索引,對于我們查詢
基于某一個字段查詢,如果那個字段加了索引

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容