Hive数据压缩和存储
一、Hadoop压缩配置
1、MR支持的压缩编码
压缩格式 | 算法 | 文件扩展名 | 是否可切分 |
---|---|---|---|
DEFLATE | DEFLATE | .deflate | 否 |
Gzip | DEFLATE | .gz | 否 |
bzip2 | bzip2 | .bz2 | 是 |
LZO | LZO | .lzo | 是 |
Snappy | Snappy | .snappy | 否 |
为了支持多种压缩/解压缩算法,Hadoop 引入了编码/解码器,如下表所示:
压缩格式 | 对应的编码/解码器 |
---|---|
DEFLATE | org.apache.hadoop.io.compress.DefaultCodec |
gzip | org.apache.hadoop.io.compress.GzipCodec |
bzip2 | org.apache.hadoop.io.compress.BZip2Codec |
LZO | com.hadoop.compression.lzo.LzopCodec |
Snappy | org.apache.hadoop.io.compress.SnappyCodec |
2、压缩参数配置
3、开启 Map 输出阶段压缩(MR 引擎)
开启 map 输出阶段压缩可以减少 job 中 map 和 Reduce task 间数据传输量。具体配置如 下:
1)案例实操:
(1)开启 hive 中间传输数据压缩功能
hive (default)>set hive.exec.compress.intermediate=true;
(2)开启 mapreduce 中 map 输出压缩功能
hive (default)>set mapreduce.map.output.compress=true;
(3)设置 mapreduce 中 map 输出数据的压缩方式
hive (default)>set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;
(4)执行查询语句
hive (default)> select count(ename) name from emp;
没用前查看Map的logs
用后查看Map的logs
若是运行MR报错:
java.lang.RuntimeException: native snappy library not available: this version of libhadoop was built without snappy support.
便把native文件夹直接拷贝到了hadoop里把原来的覆盖了
因为拷贝的话一般都是root权限 如果此时没有改变权限的话大概会错误
使用chown -R 777 文件夹
或 chown -R 当前用户 native文件夹
chown -R 当前用户:当前用户组 native文件夹
并且:配置mapred-site.xml文件。
我将 mapreduce.job.ubertask.enable 这个参数设成true了
去掉这个参数以及对应的其他两个Ubertask.maxmaps和maxreduces参数就OK了。
4、开启Reduce输出阶段压缩
当Hive将输出写入到表中时,输出内容同样可以 进行压缩。属 性 hive.exec.compress.output 控制着这个功能。用户可能需要保持默认设置文件中的默认值 false, 这样默认的输出就是非压缩的纯文本文件了。用户可以通过在查询语句或执行脚本中设置这个值为 true,来开启输出结果压缩功能。
1)案例实操:
(1)开启 hive 最终输出数据压缩功能
hive (default)>set hive.exec.compress.output=true;
(2)开启 mapreduce 最终输出数据压缩
hive (default)>set mapreduce.output.fileoutputformat.compress=true;
(3)设置 mapreduce 最终数据输出压缩方式
hive (default)> set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;
(4)设置 mapreduce 最终数据输出压缩为块压缩
hive (default)> set mapreduce.output.fileoutputformat.compress.type=BLOCK;
(5)测试一下输出结果是否是压缩文件
hive (default)> insert overwrite local directory '/opt/module/data/snappy-result' select * from emp distribute by deptno sort by empno desc;
结果:
二、主流文件存储格式对比实验
从存储文件的压缩比和查询速度两个角度对比。
1)测试数据 log.data
2)TextFile
(1)创建表,存储数据格式为 TEXTFILE
create table log_text ( 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;
(2)向表中加载数据
hive (default)> load data local inpath '/opt/module/hive/datas/log.data' into table log_text ;
(3)查看表中数据大小
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_text;
18.1 M /user/hive/warehouse/test.db/log_text/log.data
hive (test)>
3)ORC
(1)创建表,存储数据格式为 ORC
create table log_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"); -- 设置 orc 存储不使用压缩
(2)向表中加载数据
hive (default)> insert into table log_orc select * from log_text;
(3)查看表中数据大小
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_text;
18.1 M /user/hive/warehouse/test.db/log_text/log.data
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_orc;
7.7 M /user/hive/warehouse/test.db/log_orc/000000_0
4)Parquet
(1)创建表,存储数据格式为 parquet
create table log_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;
(2)向表中加载数据
hive (default)> insert into table log_parquet select * from log_text;
(3)查看表中数据大小
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_parquet;
13.1 M /user/hive/warehouse/test.db/log_parquet/000000_0
hive (test)>
存储文件的对比总结:
ORC > Parquet > textFile
存储文件的查询速度测试:
(1)TextFile
hive (default)> insert overwrite local directory '/opt/modules/testdata/log_text' select substring(url,1,4) from log_text;
(2)ORC
hive (default)> insert overwrite local directory '/opt/modules/testdata/log_orc' select substring(url,1,4) from log_orc;
(3)Parquet
hive (default)> insert overwrite local directory '/opt/modules/testdata/log_parquet' select substring(url,1,4) from log_parquet;
存储文件的查询速度总结:查询速度相近。
三、存储和压缩结合
1、测试存储和压缩
ORC 存储方式的压缩:
注意:所有关于 ORCFile 的参数都是在 HQL 语句的 TBLPROPERTIES 字段里面出现
1)创建一个ZLIB压缩的ORC存储方式
create table log_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");
(2)插入数据
insert into log_orc_zlib select * from log_text;
(3)查看插入后数据
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_orc_zlib;
2.8 M /user/hive/warehouse/test.db/log_orc_zlib/000000_0
2)创建一个SNAPPY压缩的ORC存储方式
(1)建表语句
create table log_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");
(2)插入数据
insert into log_orc_snappy select * from log_text;
(3)查看插入后数据
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_orc_snappy;
3.7 M /user/hive/warehouse/test.db/log_orc_snappy/000000_0
ZLIB 比 Snappy 压缩的还小。原因是 ZLIB 采用的是 deflate 压缩算法。比 snappy 压缩的 压缩率高。
3)创建一个SNAPPY压缩的parquet存储方式
(1)建表语句
create table log_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
tblproperties("parquet.compress"="SNAPPY");
(2)插入数据
insert into log_parquet_snappy select * from log_text;
(3)查看插入后数据
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_parquet_snappy;
13.1 M /user/hive/warehouse/test.db/log_parquet_snappy/000000_0
存储方式和压缩总结
在实际的项目开发当中,hive 表的数据存储格式一般选择:orc 或 parquet。压缩方式一 般选择 snappy,lzo。