?HIVE底層是hdfs和mapreduce實(shí)現(xiàn)存儲(chǔ)和計(jì)算的。所以HIVE可以使用hadoop自帶的InputFormat和Outputformat實(shí)現(xiàn)從不同的數(shù)據(jù)源讀取文件和寫出不同格式的文件到文件系統(tǒng)中。同理,HIVE也可以使用hadoop配置的壓縮方法對(duì)中間結(jié)果或最終數(shù)據(jù)進(jìn)行壓縮。
1.什么是壓縮及優(yōu)劣?
hive中數(shù)據(jù)做壓縮和解壓縮跟windows下的數(shù)據(jù)壓縮差不錯(cuò),同樣有很多中壓縮算法,結(jié)果是以不同的后綴名區(qū)別。使用數(shù)據(jù)壓縮好處是可以最大程度的減少文件所需的磁盤空間和網(wǎng)絡(luò)I/O的開(kāi)銷,尤其文本文件一般壓縮率可以高達(dá)40%左右,對(duì)于集群來(lái)說(shuō)帶寬是稀有資源,所有網(wǎng)絡(luò)傳輸性能的提升很重要 。但是使用壓縮和解壓縮會(huì)增加CPU的開(kāi)銷。
所以具體使不使用數(shù)據(jù)壓縮,具體取決于job類型:對(duì)那些I/O密集型的作業(yè)使用數(shù)據(jù)壓縮,cpu密集型,使用壓縮反而會(huì)降低性能。不過(guò)對(duì)于作業(yè)的類型的判斷,只能通過(guò)實(shí)際測(cè)量對(duì)比執(zhí)行結(jié)果去分析。
2.HIVE中常見(jiàn)的壓縮算法
注意,注意,注意,hive中的壓縮算法主要取決于hadoop版本。不同的版本會(huì)系統(tǒng)不同的壓縮編碼和解碼器。比如我們公司目前使用的hadoop2.9版本已經(jīng)支持了很多種壓縮方式,版本越高支持的壓縮方式越多??梢栽趆adoop下的core-site.xm文件中配置壓縮方式,hive使用的也是這個(gè)配置文件。如下是我集群中配置的的壓縮方式,實(shí)際開(kāi)發(fā)中可以根據(jù)自己需求配置壓縮方式。當(dāng)然如果不配置的話,默認(rèn)則不會(huì)使用壓縮。比如我們公司就沒(méi)有配置使用snappy壓縮方法。
<property>
<name>io.compression.codecs</name>
<value>org.apache.hadoop.io.compress.GzipCodec,org.apache.hadoop.io.compress.DefaultCodec,com.hadoop.compression.lzo.LzoCodec,com.hadoop.compression.lzo.LzopCodec,org.apache.hadoop.io.compress.BZip2Codec</value>
</property>
可以通過(guò)如下命令查看hive中已經(jīng)配置好的壓縮算法。使用set命令可以查看所有hive配置文件中的屬性值以及hive安裝環(huán)境的hadoop文件的屬性值。hive中默認(rèn)壓縮是關(guān)閉的,可以通過(guò)set hive.exec.compress.output來(lái)查看
hive (fdm_sor)> set io.compression.codecs;
io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec,
org.apache.hadoop.io.compress.DefaultCodec,
com.hadoop.compression.lzo.LzoCodec,
com.hadoop.compression.lzo.LzopCodec,
org.apache.hadoop.io.compress.BZip2Codec
如上查詢的結(jié)果是對(duì)應(yīng)的算法在hadoop底層的類,為什么要有不同壓縮算法呢?主要是因?yàn)椴煌膲嚎s算法在壓縮率和壓縮時(shí)間,壓縮的文件是否可切分等方面都不同,實(shí)際開(kāi)發(fā)中需要根據(jù)實(shí)際情況權(quán)衡使用。
壓縮格式 | 對(duì)應(yīng)的類 | 文件擴(kuò)展名 | 是否支持多文件 | 文件可分割性 |
DEFLATE (默認(rèn)) | org.apache.hadoop.io.compress.DefaultCodec | .deflate | 不 | 不 |
gzip | org.apache.hadoop.io.compress.GzipCodec | .gz | 不 | 不 |
bzip2 | org.apache.hadoop.io.compress.BZip2Codec | .bz2 | 不 | 是 |
LZO | com.hadoop.compression.lzo.LzopCodec | .lzo_deflate | 不 | 是 |
Lzop | com.hadoop.compression.lzo.LzopCodec; | .lzo | 不 | 是 |
三.HIVE中壓縮算法性能分析
這里測(cè)試的表中文件是516.4MB,hadoop環(huán)境的塊設(shè)置大小是256Mb,正好這樣數(shù)據(jù)存儲(chǔ)是分塊存儲(chǔ),計(jì)算有IO的開(kāi)銷。可以測(cè)算不同壓縮算法下數(shù)據(jù)傳輸計(jì)算的時(shí)間,以及壓縮率等因子。
[robot~]hadoop fs -du h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201901
516.4 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201901/201901.txt
這是直接從linux本地load的文件到hdfs上,文件實(shí)際數(shù)據(jù)大小是516.4Mb
1.hive中不使用壓縮,進(jìn)行計(jì)算與存儲(chǔ)
--1.無(wú)壓縮算法下進(jìn)行數(shù)據(jù)存儲(chǔ)計(jì)算。
set hive.exec.compress.output=false; --默認(rèn)就是false的
insert overwrite table t_fin_demo partition(staits_date ='201900')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.使用du -h命令查看hdfs上文件存儲(chǔ)情況
[finance@master2-dev software]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201900
271.0 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201900/000000_0
271.0 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201900/000001_0
4.7 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201900/000002_0
3.程序運(yùn)行時(shí)間
Total MapReduce CPU Time Spent: 54 seconds 200 msec
Time taken: 36.445 seconds
總結(jié):從上面數(shù)據(jù)看出,無(wú)壓縮模式下,數(shù)據(jù)存儲(chǔ)的格式就是文本格式,無(wú)后綴名,可以直接從-cat查看。文件存儲(chǔ)大小是原本文件的大小271+271+4.7=546.7Mb,運(yùn)行時(shí)間是36.445。
2.使用hive默認(rèn)的壓縮方式,hive存儲(chǔ)的文件后綴是.deflate
1.使用deflate進(jìn)行壓縮
set hive.exec.compress.output=true;
--true是開(kāi)啟壓縮,默認(rèn)是關(guān)閉的,如果開(kāi)啟后不指定壓縮方式,默認(rèn)使用defalte。
set mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.DefaultCodec;
insert overwrite table t_fin_demo partition(staits_date ='201904')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.查看數(shù)據(jù)存儲(chǔ)和計(jì)算情況
[finance@master2-dev hadoop]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201903
75.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201903/000000_0.deflate
75.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201903/000001_0.deflate
1.3 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201903/000002_0.deflate
3.程序耗時(shí)時(shí)間:
Time taken: 54.659 seconds
總結(jié):上面數(shù)據(jù)看,使用默認(rèn)的的deflate壓縮算法,數(shù)據(jù)存儲(chǔ)文件后綴名為.deflate.文件存儲(chǔ)的大小是:75.9+75.9+1.3=153.1。程序耗時(shí)是54.659s.可以看出deflate壓縮的壓縮率很高,但是程序耗時(shí)相比不壓縮有所上升。
3.使用gzip進(jìn)行壓縮,hive中文件后綴是.gz
1.使用Gzip進(jìn)行壓縮存儲(chǔ)
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.GzipCodec;
insert overwrite table t_fin_demo partition(staits_date ='201904')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.使用du -h命令查看hdfs上文件存儲(chǔ)情況
[finance@master2-dev hadoop]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201904
75.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201904/000000_0.gz
75.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201904/000001_0.gz
1.3 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201904/000002_0.gz
3.程序運(yùn)行時(shí)間
Total MapReduce CPU Time Spent: 1 minutes 33 seconds 430 msec
OK
Time taken: 62.436 seconds
總結(jié):上面數(shù)據(jù)看,使用默認(rèn)的的gzip壓縮算法,數(shù)據(jù)存儲(chǔ)文件后綴名為.gz文件存儲(chǔ)的大小是:75.9+75.9+1.3=153.1。程序耗時(shí)是62.436.如果下載到windows本地解壓后可讀
4.使用lzo壓縮算法進(jìn)行壓縮,文件后綴是.lzo_deflate
1.使用lzo進(jìn)行壓縮存儲(chǔ)
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress.codec=com.hadoop.compression.lzo.LzoCodec;
insert overwrite table t_fin_demo partition(staits_date ='201905')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.使用du -h命令查看hdfs上文件存儲(chǔ)情況
[finance@master2-dev hadoop]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201905
121.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201905/000000_0.lzo_deflate
121.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201905/000001_0.lzo_deflate
2.1 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201905/000002_0.lzo_deflate
3.程序運(yùn)行時(shí)間
Total MapReduce CPU Time Spent: 58 seconds 700 msec
OK
Time taken: 42.45 seconds
總結(jié):上面數(shù)據(jù)看,使用默認(rèn)的的lzo壓縮算法,數(shù)據(jù)存儲(chǔ)文件后綴名為.lzo_deflate.文件存儲(chǔ)的大小是:121.9+121.9+2.1=245.9。程序耗時(shí)是42.45s。
5.使用Lzop壓縮方式,hive存儲(chǔ)的文件后綴是.lzo
1.使用lzop進(jìn)行壓縮存儲(chǔ)
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress.codec=com.hadoop.compression.lzo.LzopCodec;
insert overwrite table t_fin_demo partition(staits_date ='201906')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.使用du -h命令查看hdfs上文件存儲(chǔ)情況
[finance@master2-dev hadoop]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201906
121.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201906/000000_0.lzo
121.9 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201906/000001_0.lzo
2.1 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201906/000002_0.lzo
3.程序運(yùn)行時(shí)間
Total MapReduce CPU Time Spent: 47 seconds 280 msec
OK
Time taken: 34.439 seconds
總結(jié):上面數(shù)據(jù)看,使用默認(rèn)的的Lzop壓縮算法,數(shù)據(jù)存儲(chǔ)文件后綴名為.lzo。文件存儲(chǔ)的大小是:121.9+121.9+2.1=245.9。程序耗時(shí)是34.439s。
6.使用BZip2壓縮方式,hive存儲(chǔ)的文件后綴是.bz2
1.使用Bzip2進(jìn)行壓縮存儲(chǔ)
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.BZip2Codec;
insert overwrite table t_fin_demo partition(staits_date ='201907')
select
name,
id_type,
idcard,
org,
loan_no,
busi_type,
busi_category,
open_date,
dure_date,
loan_amount,
happen_time,
amout,
due_amt,
stat
from t_fin_demo where staits_date ='201901';
2.使用du -h命令查看hdfs上文件存儲(chǔ)情況
[finance@master2-dev hadoop]$ hadoop fs -du -h /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201907
52.5 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201907/000000_0.bz2
52.5 M /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201907/000001_0.bz2
935.2 K /user/finance/hive/warehouse/fdm_sor.db/t_fin_demo/staits_date=201907/000002_0.bz2
3.程序運(yùn)行時(shí)間
Total MapReduce CPU Time Spent: 2 minutes 47 seconds 530 msec
OK
Time taken: 96.42 seconds
總結(jié):上面數(shù)據(jù)看,使用默認(rèn)的的Bzip2壓縮算法,數(shù)據(jù)存儲(chǔ)文件后綴名為.bz2。文件存儲(chǔ)的大小是:52.5+52.5+0.934=106Mb。程序耗時(shí)是96.42s
各種壓縮算法綜合分析
壓縮方式 | 原文件大小 | 壓縮后文件 | 運(yùn)行時(shí)間 | 備注 |
不使用壓縮 | 516.4Mb | 546.70Mb | 36.445s |
|
defalte(默認(rèn)壓縮) | 516.4Mb | 153.16Mb | 54.659s | 壓縮率高 |
lzo壓縮 | 516.4Mb | 245.90Mb | 42.45s | LZO壓縮和解壓縮很快,但是壓縮的文件較大 |
Lzop壓縮 | 516.4Mb | 249.90Mb | 34.439s | LZOP壓縮和解壓縮很快,但是壓縮的文件較大 |
gzip壓縮 | 516.4Mb | 153.16Mb | 62.436s | GZip 和 BZip2壓縮可以保證最小的壓縮文件,但是過(guò)于消耗時(shí)間,非常不適合CPU型運(yùn)算。 |
Bzip2壓縮 | 516.4Mb | 106.00Mb | 96.42s | GZip 和 BZip2壓縮可以保證最小的壓縮文件,但是過(guò)于消耗時(shí)間,不適合CPU型運(yùn)算。 |
綜合上表可以看出,每種壓縮算法都有自己的優(yōu)缺點(diǎn)。具體使用哪種壓縮取決于具體存儲(chǔ)的數(shù)據(jù)格式與計(jì)算模式有關(guān)。具體壓縮使用與原理參考后續(xù)博客。
1.就壓縮比來(lái)說(shuō):bzip2>gzip>deflate>lzo,故bzip2最節(jié)省存儲(chǔ)空間,但是耗時(shí)高哇。
2.解壓速度和耗時(shí):lzo>deflate>gzip>bzip2
本文轉(zhuǎn)載自微信公眾號(hào)「滌生大數(shù)據(jù)」,作者「滌生大數(shù)據(jù)」,可以通過(guò)以下二維碼關(guān)注。

轉(zhuǎn)載本文請(qǐng)聯(lián)系「滌生大數(shù)據(jù)」公眾號(hào)。