码迷,mamicode.com
首页 > 其他好文 > 详细

Hadoop基础(五十):压缩和存储(二)

时间:2020-07-24 21:30:31      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:开启   apr   zlib   任务   offset   org   foo   overwrite   visio   

4 开启 Reduce 输出阶段压缩

当 Hive 将 输 出 写 入 到 表 中 时 , 输 出 内 容 同 样 可 以 进 行 压 缩 。 属 性hive.exec.compress.output 控制着这个功能。用户可能需要保持默认设置文件中的默认值 false,
这样默认的输出就是非压缩的纯文本文件了。用户可以通过在查询语句或执行脚本中设置这个值为 true,来开启输出结果压缩功能。
案例实操:
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/datas/distribute-result‘ select * from emp distribute by deptno sort 
by empno desc;

5 文件存储格式

Hive 支持的存储数据的格式主要有:TEXTFILE 、SEQUENCEFILE、ORC、PARQUET。
5.1 列式存储和行式存储
技术图片

 

 

图 6-10 列式存储和行式存储
如图 6-10 所示左边为逻辑表,右边第一个为行式存储,第二个为列式存储。
1.行存储的特点
查询满足条件的一整行数据的时候,列存储则需要去每个聚集的字段找到对应的每个列的值,行存储只需要找到其中一个值,其余的值都在相邻地方,所以此时行存储查询的速度更快。
2.列存储的特点
因为每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的数据量;每个字段的数据类型一定是相同的,列式存储可以针对性的设计更好的设计压缩算法
TEXTFILE 和 SEQUENCEFILE 的存储格式都是基于行存储的;
ORC 和 PARQUET 是基于列式存储的。
5.2 TextFile 格式
默认格式,数据不做压缩,磁盘开销大,数据解析开销大。可结合 Gzip、Bzip2 使用,但使用 Gzip 这种方式,hive 不会对数据进行切分,从而无法对数据进行并行操作。
5.3 Orc 格式
Orc (Optimized Row Columnar)是 Hive 0.11 版里引入的新的存储格式。
如图 6-11 所示可以看到每个 Orc 文件由 1 个或多个 stripe 组成,每个 stripe 一般为HDFS 的块大小,每一个 stripe 包含多条记录,这些记录按照列进行独立存储,对应到
Parquet 中的 row group 的概念。每个 Stripe 里有三部分组成,分别是 Index Data,RowData,Stripe Footer:
技术图片

 

 

图 6-11 Orc 格式
1)Index Data:一个轻量级的 index,默认是每隔 1W 行做一个索引。这里做的索引应该只是记录某行的各字段在 Row Data 中的 offset。
2)Row Data:存的是具体的数据,先取部分行,然后对这些行按列进行存储。对每个列进行了编码,分成多个 Stream 来存储。
3)Stripe Footer:存的是各个 Stream 的类型,长度等信息。
每个文件有一个 File Footer,这里面存的是每个 Stripe 的行数,每个 Column 的数据类型信息等;每个文件的尾部是一个 PostScript,这里面记录了整个文件的压缩类型以及
FileFooter 的长度信息等。在读取文件时,会 seek 到文件尾部读 PostScript,从里面解析到File Footer 长度,再读 FileFooter,从里面解析到各个 Stripe 信息,再读各个 Stripe,即从
后往前读。
 
5.4 Parquet 格式
Parquet 文件是以二进制方式存储的,所以是不可以直接读取的,文件中包括该文件的数据和元数据,因此 Parquet 格式文件是自解析的。
1) 行组(Row Group):每一个行组包含一定的行数,在一个 HDFS 文件中至少存储一个行组,类似于 orc 的 stripe 的概念。
2) 列块(Column Chunk):在一个行组中每一列保存在一个列块中,行组中的所有列连续的存储在这个行组文件中。一个列块中的值都是相同类型的,不同的列块可能
使用不同的算法进行压缩。
3) 页(Page):每一个列块划分为多个页,一个页是最小的编码的单位,在同一个列块的不同页可能使用不同的编码方式。
 
通常情况下,在存储 Parquet 数据的时候会按照 Block 大小设置行组的大小,由于一般情况下每一个 Mapper 任务处理数据的最小单位是一个 Block,这样可以把每一个行组由一
个 Mapper 任务处理,增大任务执行并行度。Parquet 文件的格式如图 6-12 所示。
技术图片
上图展示了一个 Parquet 文件的内容,一个文件中可以存储多个行组,文件的首位都是该文件的 Magic Code,用于校验它是否是一个 Parquet 文件,Footer length 记录了文件元数
据的大小,通过该值和文件长度可以计算出元数据的偏移量,文件的元数据中包括每一个行组的元数据信息和该文件存储数据的 Schema 信息。除了文件中每一个行组的元数据,每一
页的开始都会存储该页的元数据,在 Parquet 中,有三种类型的页:数据页、字典页和索引页。。数据页用于存储当前行组中该列的值,字典页存储该列值的编码字典,每一个列块中最
多包含一个字典页,索引页用来存储当前行组下该列的索引,目前 Parquet 中还不支持索引页。
5.5 主流文件存储格式对比实验
从存储文件的压缩比和查询速度两个角度对比。
存储文件的压缩比测试:
1. 测试数据
技术图片
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/datas/log.data into table log_text ;
(3)查看表中数据大小
hive (default)> dfs -du -h /user/hive/warehouse/log_text;
18.1 M /user/hive/warehouse/log_text/log.data
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 ;
(2)向表中加载数据
hive (default)> insert into table log_orc select * from log_text ;
(3)查看表中数据大小
hive (default)> dfs -du -h /user/hive/warehouse/log_orc/ ;
2.8 M /user/hive/warehouse/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 (default)> dfs -du -h /user/hive/warehouse/log_parquet/ ;
13.1 M /user/hive/warehouse/log_parquet/000000_0
存储文件的压缩比总结:
ORC > Parquet > textFile
存储文件的查询速度测试:
1.TextFile
hive (default)> select count(*) from log_text;
_c0
100000
Time taken: 21.54 seconds, Fetched: 1 row(s)
Time taken: 21.08 seconds, Fetched: 1 row(s)
Time taken: 19.298 seconds, Fetched: 1 row(s)
2.ORC
hive (default)> select count(*) from log_orc;
_c0
100000
Time taken: 20.867 seconds, Fetched: 1 row(s)
Time taken: 22.667 seconds, Fetched: 1 row(s)
Time taken: 18.36 seconds, Fetched: 1 row(s)
3.Parquet
hive (default)> select count(*) from log_parquet;
_c0
100000
Time taken: 22.922 seconds, Fetched: 1 row(s)
Time taken: 21.074 seconds, Fetched: 1 row(s)
Time taken: 18.384 seconds, Fetched: 1 row(s)
存储文件的查询速度总结:查询速度相近。
 

6 存储和压缩结合

6.1 修改 Hadoop 集群具有 Snappy 压缩方式
1.查看 hadoop checknative 命令使用
[atguigu@hadoop104 hadoop-2.7.2]$ hadoop
 checknative [-a|-h] check native hadoop and compression libraries 
availability
2.查看 hadoop 支持的压缩方式
[atguigu@hadoop104 hadoop-2.7.2]$ hadoop checknative
17/12/24 20:32:52 WARN bzip2.Bzip2Factory: Failed to load/initialize native-bzip2 
library system-native, will use pure-Java version
17/12/24 20:32:52 INFO zlib.ZlibFactory: Successfully loaded & initialized 
native-zlib library
Native library checking:
hadoop: true /opt/module/hadoop-2.7.2/lib/native/libhadoop.so
zlib: true /lib64/libz.so.1
snappy: false 
lz4: true revision:99
bzip2: false
3.将编译好的支持 Snappy 压缩的 hadoop-2.7.2.tar.gz 包导入到 hadoop102 的
/opt/software 中
4.解压 hadoop-2.7.2.tar.gz 到当前路径
[atguigu@hadoop102 software]$ tar -zxvf hadoop-2.7.2.tar.gz
5.进入到/opt/software/hadoop-2.7.2/lib/native 路径可以看到支持 Snappy 压缩的动态链接库
[atguigu@hadoop102 native]$ pwd
/opt/software/hadoop-2.7.2/lib/native
[atguigu@hadoop102 native]$ ll
-rw-r--r--. 1 atguigu atguigu 472950 91 10:19 libsnappy.a
-rwxr-xr-x. 1 atguigu atguigu 955 91 10:19 libsnappy.la
lrwxrwxrwx. 1 atguigu atguigu 18 1224 20:39 libsnappy.so -> libsnappy.so.1.3.0
lrwxrwxrwx. 1 atguigu atguigu 18 1224 20:39 libsnappy.so.1 -> libsnappy.so.1.3.0
-rwxr-xr-x. 1 atguigu atguigu 228177 91 10:19 libsnappy.so.1.3.0
6.拷贝/opt/software/hadoop-2.7.2/lib/native 里面的所有内容到开发集群 的/opt/module/hadoop-2.7.2/lib/native 路径上
[atguigu@hadoop102 native]$ cp ../native/* /opt/module/hadoop-2.7.2/lib/native/
7.分发集群
[atguigu@hadoop102 lib]$ xsync native/
8.再次查看 hadoop 支持的压缩类型
[atguigu@hadoop102 hadoop-2.7.2]$ hadoop checknative
17/12/24 20:45:02 WARN bzip2.Bzip2Factory: Failed to load/initialize native-bzip2 
library system-native, will use pure-Java version
17/12/24 20:45:02 INFO zlib.ZlibFactory: Successfully loaded & initialized 
native-zlib library
Native library checking:
hadoop: true /opt/module/hadoop-2.7.2/lib/native/libhadoop.so
zlib: true /lib64/libz.so.1
snappy: true /opt/module/hadoop-2.7.2/lib/native/libsnappy.so.1
lz4: true revision:99
bzip2: false
9.重新启动 hadoop 集群和 hive
6.2 测试存储和压缩
官网:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ORC
ORC 存储方式的压缩:
技术图片
1.创建一个非压缩的的 ORC 存储方式
(1)建表语句
create table log_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");
(2)插入数据
hive (default)> insert into table log_orc_none select * from log_text ;
(3)查看插入后数据
hive (default)> dfs -du -h /user/hive/warehouse/log_orc_none/ ;
7.7 M /user/hive/warehouse/log_orc_none/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)插入数据
hive (default)> insert into table log_orc_snappy select * from log_text ;
(3)查看插入后数据
hive (default)> dfs -du -h /user/hive/warehouse/log_orc_snappy/ ;
3.8 M /user/hive/warehouse/log_orc_snappy/000000_0
3.上一节中默认创建的 ORC 存储方式,导入数据后的大小为
2.8 M /user/hive/warehouse/log_orc/000000_0
比 Snappy 压缩的还小。原因是 orc 存储文件默认采用 ZLIB 压缩,ZLIB 采用的是deflate 压缩算法。比 snappy 压缩的小。
4.存储方式和压缩总结
在实际的项目开发当中,hive 表的数据存储格式一般选择:orc 或 parquet。压缩方式一般选择 snappy,lzo。
 

Hadoop基础(五十):压缩和存储(二)

标签:开启   apr   zlib   任务   offset   org   foo   overwrite   visio   

原文地址:https://www.cnblogs.com/qiu-hua/p/13368443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!