概述
为什么会出现需要对文件进行压缩?
在Hadoop中,文件需要存储、传输、读取磁盘、写入磁盘等等操作,而文件的大小,直接决定了这些这些操作的速度。
-
常见压缩方式、压缩比、压缩解压缩时间、是否可切分
原文件:1403M
Snappy 压缩:701M,压缩时间:6.4s,解压时间:19.8s,不可切分
LZ4 压缩:693M,压缩时间:6.4s,解压时间:2.36s,不可切分
LZO 压缩:684M,压缩时间:7.6s,解压时间:11.1s,带序号可切分
GZIP 压缩:447M,压缩时间:85.6s,解压时间:21.8s,不可切分
BZIP2:压缩:390M,压缩时间:142.3s,解压时间:62.5s,可切分
总结:压缩比和压缩时间成反比,压缩比越小,耗费时间越大
-
两个矛盾:
耗费CPU、时间与存储空间、传输速度、IO的矛盾
压缩比与压缩、解压缩时间的矛盾
ps:追求合适场景使用合适方式
图解MapReduce

1.第一次传入压缩文件,应选用可以切片的压缩方式,否则整个文件将只有一个Map执行
2.第二次压缩应选择压缩解压速度快的压缩方式
3.第三次压缩有两种场景分别是:一.当输出文件为下一个job的输入,选择可切分的压缩方式例如:BZip2。二.当输出文件直接存到HDFS,选择压缩比高的压缩方式。
-
hive参数
hive.exec.compress.output 设置是否压缩
mapreduce.output.fileoutputformat.compress.codec 设置压缩Reduce类型输出
hive.intermediate.compression.codec 设置中间Map压缩类型
可选类型:
org.apache.hadoop.io.compress.DefaultCodec
org.apache.hadoop.io.compress.GzipCodec
org.apache.hadoop.io.compress.BZip2Codec
com.hadoop.compression.lzo.LzoCodec
org.apache.hadoop.io.compress.Lz4Codec
org.apache.hadoop.io.compress.SnappyCodec
定义压缩方式的选择
1) 写进相关配置文件中
core-site.xml codecs<property><name>io.compression.codecs</name><value>org.apache.hadoop.io.compress.GzipCodec,org.apache.hadoop.io.compress.DefaultCodec,org.apache.hadoop.io.compress.BZip2Codec,org.apache.hadoop.io.compress.SnappyCodec</value></property> mapred-site.xml switch+codec<property><name>mapreduce.output.fileoutputformat.compress</name><value>true</value></property><property><name>mapreduce.output.fileoutputformat.compress.codec</name><value>org.apache.hadoop.io.compress.BZip2Codec</value></property> 2) 第二种方式 在 hive 客户端代码层实现
mapreduce.map.output.fileoutputformat.compress
SET hive.exec.compress.output=true;
SET mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.BZip2Codec;
create table page_views_bzip2
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
as select * from page_views;
create table page_views_snappy
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t"
as select * from page_views;
===存储格式在Hadoop 和hive 中应用===
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)
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
行式存储
1)一行记录的所有记录字段都存放在一个block中
2)多列加载
3)压缩比
列式存储
1)只加载需要的列
2) 更高的压缩比
缺点:重构
总结:
1) 磁盘IO、网络IO
2) 节省空间
-->> 提高查询性能(向量化)
predicate pushdown (谓词下压--类似Oracle优化中的谓词推入)
数据存储
* 按行存储数据
SEQUENCEFILE /TEXTFILE
* 按列存储数据
RC/ORC/PARQUET/AVRO
====TEXTFILE 表 ===
create table page_views(
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 TEXTFILE ;
load data local inpath '/opt/datas/page_views.data' into table page_views ;
dfs -du -h /user/hive/warehouse/page_views/ ;
18.1 M /user/hive/warehouse/page_views/page_views.data
>>>>>>>orc
2) orc 格式存储
File formats are specified at the table (or partition) level. You can specify the ORC file format with HiveQL statements such as these:
CREATE TABLE ... STORED AS ORC
ALTER TABLE ... [PARTITION partition_spec] SET FILEFORMAT ORC
SET hive.default.fileformat=Orc
The parameters are all placed in the TBLPROPERTIES (see Create Table). They are:
RC三种创建/使用方式:
1, STORED AS ORC;
2, ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' with serdeproperties('serialization.null.format' = '') STORED AS ORC;
3, ROW FORMAT DELIMITED NULL DEFINED AS '' STORED AS ORC;
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 ;
文件格式数据不能直接导入到seq或是RC或是orc ,
必须要先讲文本格式数据导入到textfile表中,再insert into 到seq/rc/orc 表中
insert into table page_views_orc select * from page_views ;
dfs -du -h /user/hive/warehouse/page_views_orc/ ;
2.6 M /user/hive/warehouse/page_views_orc/000000_0
>>>>>>>> parquet
create table page_views_parquet(
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 PARQUET ;
insert into table page_views_parquet select * from page_views ;
dfs -du -h /user/hive/warehouse/page_views_parquet/ ;
13.1 M /user/hive/warehouse/page_views_parquet/000000_0
select session_id,count(*) cnt from page_views group by session_id order by cnt desc limit 30 ; --用时41s 左右
select session_id,count(*) cnt from page_views_orc group by session_id order by cnt desc limit 30 --- ;用时61s(可能是笔记本IO 跟不上)
select session_id,count(*) cnt from page_views_parquet group by session_id order by cnt desc limit 30 ; --- 用时 38s
-------
select session_id from page_views limit 30 ; --0.051s
select session_id from page_views_orc limit 30 ; --0.077s
select session_id from page_views_parquet limit 30 ; -- 0.053
以上测试结果都在笔记本的虚拟机中进行的,IO跟不上所以差异性不是很大 ,生产环境中的结果 可能会有很大的提升
====== 压缩 ===
压缩在大数据中的使用
为什么需要压缩?
1) 存储
2) 网络/磁盘IO
压缩在Hadoop中的应用--- 是否支持分割 , 如下图所示
如上图所示,只有 bzip2 和LZO 支持分割,
压缩在Hadoop中的应用---压缩在MapReduce中的应用 如下图所示
MapReduce 几个阶段中 可能用到的压缩阶段有几个阶段--
map阶段--->>reduce
压缩在Hadoop中的应用---压缩在MapReduce中的应用 如下图所示
由于原生的Hadoop 不支持snapy等压缩方式,需要编译Hadoop
编译Hadoop 参考 http://blog.****.net/wjl7813/article/details/79101837
18/02/08 17:08:13 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native
18/02/08 17:08:13 INFO zlib.ZlibFactory: Successfully loaded & initialized native-zlib library
Native library checking:
hadoop: true /home/hadoop/app/hadoop-2.6.0-cdh5.7.0/lib/native/libhadoop.so.1.0.0
zlib: true /lib64/libz.so.1
snappy: true /usr/lib64/libsnappy.so.1
lz4: true revision:99
bzip2: true /lib64/libbz2.so.1
openssl: true /usr/lib64/libcrypto.so
Compressionin Hive

参考文档
https://cwiki.apache.org/confluence/display/Hive/Home
当然,
set mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec;
和
set mapred.output.compression.codec=org.apache.hadoop.io.compress.LzopCodec
两者一样,是LzopCodec的两个不同开源包。用哪个都行。
hive压缩设置
1)中间结果压缩
中间结果是map产生的。格式设置语句
set hive.exec.compress.intermediate=true;
set hive.intermediate.compression.codec=org.apache.Hadoop.io.compress.LzoCodec;
map结果压缩最好使用snappy的,因为压缩的前提是map输出非常大,影响io,如果中间结果数据集比较小反而会拖慢速度
另外,中间结果的压缩格式设置还可以直接设置map输出结果压缩实现,如
set mapred.map.output.compression.codec=org.apache.Hadoop.io.compress.SnappyCodec
来代替set hive.intermediate.compression.codec这个语句实现
2)最终输出结果压缩
配置参数为hive.exec.compress.output
选择编解码器(压缩格式)参数mapred.output.compression.code(
命令格式
set hive.exec.compress.output=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
(也可以用org.apache.hadoop.io.compress.SnappyCodec)
或者
set mapred.output.compress=true
setmapred.output.compression.codec=org.apache.hadoop.io.compress.LzopCodec
两种方式功能一样,之所以两个方式,是因为作用不同的参数文件
hive.exec.compress.output和mapred.output.compression.codec是hive-site.xml中的配置参数
而mapred.output.compress 和mapred.output.compression.codec 是hdfs-site.xml的配置参数
都可以配置实现。可以查看各个文件中的配置参数,如
Orc的压缩格式设置方法为:orc.compress=SNAPPY,默认为ZLIB.
create table page_views_orc_snappy(
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"="SNAPPY");
insert into table page_views_orc_snappy select * from page_views ;
hive (default)> dfs -du -h /user/hive/warehouse/page_views_orc_snappy/ ;
3.8 M 3.8 M /user/hive/warehouse/page_views_orc_snappy/000000_0
--------------创建一个没有使用orc 压缩格式的表--
create table page_views_orc_none(
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");
insert into table page_views_orc_none select * from page_views ;
hive (default)> dfs -du -h /user/hive/warehouse/page_views_orc_none/ ;
7.7 M 7.7 M /user/hive/warehouse/page_views_orc_none/000000_0
----创建一个使用ZLIB压缩格式的ORC表
create table page_views_orc_zlib(
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"="ZLIB");
insert into table page_views_orc_zlib select * from page_views ;
hive (default)> dfs -du -h /user/hive/warehouse/page_views_orc_zlib/ ;
2.8 M 2.8 M /user/hive/warehouse/page_views_orc_zlib/000000_0
--------------------
set parquet.compression=SNAPPY ;
create table page_views_parquet_snappy(
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 parquet;
insert into table page_views_parquet_snappy select * from page_views ;
dfs -du -h /user/hive/warehouse/page_views_parquet_snappy/ ;
总结:
在实际的项目开发当中,hive表的数据
* 存储格式
orc / qarquet
* 数据压缩
snappy/LZO/bzip
总结:
1)为什么压缩
2) 如何选择压缩
3)压缩率大概是多少
4)如何在MR中使用压缩
5)如何在hive中使用压缩(hive-site.xml 层面,执行SQL层面,个人推荐SQL层次使用压缩)