Hive之数据压缩和存储格式
关于Hadoop的压缩参看之前写的博客:https://blog.****.net/zuodaoyong/article/details/104115608
本文主要关注Hive的压缩数据相关内容
通过命令查看集群支持哪些压缩编码格式:
[[email protected] native]# hadoop checknative
20/02/04 17:46:36 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native
20/02/04 17:46:36 INFO zlib.ZlibFactory: Successfully loaded & initialized native-zlib library
Native library checking:
hadoop: true /opt/hadoop/hadoop-2.8.5/lib/native/libhadoop.so.1.0.0
zlib: true /lib64/libz.so.1
snappy: true /opt/hadoop/hadoop-2.8.5/lib/native/libsnappy.so.1
lz4: true revision:10301
bzip2: true /lib64/libbz2.so.1
下文以snappy压缩为例
1、开启 Map 输出阶段压缩
(1)开启 hive 中间传输数据压缩功能
hive (test)> set hive.exec.compress.intermediate=true;
hive (test)>
(2)开启 mapreduce 中 map 输出压缩功能
hive (test)> set mapreduce.map.output.compress=true;
hive (test)>
(3)设置 mapreduce 中 map 输出数据的压缩方式
hive (test)> set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;
hive (test)>
2、开启 Reduce 输出阶段压缩
(1)开启 hive 最终输出数据压缩功能
hive (test)> set hive.exec.compress.output=true;
hive (test)>
(2)开启 mapreduce 最终输出数据压缩
hive (test)> set mapreduce.output.fileoutputformat.compress=true;
hive (test)>
(3)设置 mapreduce 最终数据输出压缩方式
hive (test)> set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;
hive (test)>
(4)设置 mapreduce 最终数据输出压缩为块压缩
hive (test)> set mapreduce.output.fileoutputformat.compress.type=BLOCK;
hive (test)>
(5)测试一下输出结果是否是压缩文件
insert overwrite local directory '/opt/temp/hive/compass' select count(1) from stu;
查看compass目录:
[[email protected] compass]# ll
总用量 4
-rw-r--r--. 1 root root 13 2月 4 18:07 000000_0.snappy
3、文件存储格式 (TEXTFILE 、SEQUENCEFILE、ORC、PARQUET)
(1)列式存储和行式存储
逻辑视图:
行存储(TEXTFILE 和 SEQUENCEFILE ):
查询满足条件的一整行数据的时候,列存储则需要去每个聚集的字段找到对应的每个列
的值,行存储只需要找到其中一个值,其余的值都在相邻地方,所以此时行存储查询的速度
更快。
列存储(ORC 和 PARQUET ):
因为每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的
数据量;每个字段的数据类型一定是相同的,列式存储可以针对性的设计更好的设计压缩算
法。
(2) TextFile 格式
默认格式,数据不做压缩,磁盘开销大,数据解析开销大。可结合 Gzip、Bzip2 使用,
注意:使用 Gzip 这种方式,hive 不会对数据进行切分,从而无法对数据进行并行操作。
(3) Orc 格式
Hive 0.11 版里引入的新的存储格式。
每个 Orc 文件由 1 个或多个 stripe 组成,每个 stripe250MB 大小,这个 Stripe 实际相当于 RowGroup 概念,不过大小由 4MB->250MB,这样应该能提升顺序读的吞吐率。每个 Stripe 里有三部分组成,分别是 Index Data,Row Data,Stripe Footer
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,即从后往前读。
(4)Parquet 格式
Parquet 是面向分析型业务的列式存储格式,由 Twitter 和 Cloudera 合作开发,2015 年 5月从 Apache 的孵化器里毕业成为 Apache 顶级项目。
Parquet 文件是以二进制方式存储的,所以是不可以直接读取的,文件中包括该文件的数据和元数据,因此 Parquet 格式文件是自解析的。
通常情况下,在存储 Parquet 数据的时候会按照 Block 大小设置行组的大小,由于一般情况下每一个 Mapper 任务处理数据的最小单位是一个 Block,这样可以把每一个行组由一个 Mapper 任务处理,增大任务执行并行度。
一个文件中可以存储多个行组,文件的首位都是该文件的 Magic Code,用于校验它是否是一个 Parquet 文件,Footer length 记录了文件元数据的大小,通过该值和文件长度可以计算出元数据的偏移量,文件的元数据中包括每一个行组的元数据信息和该文件存储数据的 Schema 信息。除了文件中每一个行组的元数据,每一页的开始都会存储该页的元数据,在 Parquet 中,有三种类型的页:数据页、字典页和索引页。数据页用于存储当前行组中该列的值,字典页存储该列值的编码字典,每一个列块中最多包含一个字典页,索引页用来存储当前行组下该列的索引,目前 Parquet 中还不支持索引页。
(5)测试上述的存储格式存储相同数据的占用磁盘大小
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;
导入测试数据:
load data local inpath '/opt/temp/hive/log.data' into table log_text;
结果:
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_text;
18.1 M /user/hive/warehouse/test.db/log_text/log.data
2)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 ;
导入测试数据:
insert into table log_orc select * from log_text ;
结果:
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_orc;
2.8 M /user/hive/warehouse/test.db/log_orc/000000_0
3)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;
导入数据:
insert into table log_parquet select * from log_text;
结果:
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_parquet;
13.1 M /user/hive/warehouse/test.db/log_parquet/000000_0
(6)ORC存储格式和snappy压缩方式结合使用
ORC 默认存储方式的压缩:
1)创建一个非压缩的的 ORC 存储方式
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");
导入数据:
insert into table log_orc_none select * from log_text;
结果:
hive (test)> dfs -du -h /user/hive/warehouse/test.db/log_orc_none;
7.7 M /user/hive/warehouse/test.db/log_orc_none/000000_0
2)创建一个 SNAPPY 压缩的 ORC 存储方式
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");
导入数据:
insert into table log_orc_snappy select * from log_text;
结果:
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
总结:
hive 表的数据存储格式一般选择:orc 或 parquet。压缩方式一 般选择 snappy,lzo。