Hive Learning (eight) data compression and data storage format

A, Hive data compression

  1.1 MR supported compression coding

  1.2 press configuration parameters

  1.3 output stage compression turned on Map

  1.4 Reduce output stage compression is turned on

Two, Hive data storage format

  2.1 line storage memory and

  2.2 common data storage format

Third, file storage format and compression combined

  3.1 compression ratio, and query speed comparison

  3.2 ORC compressed format specified storage

 

 

A, Hive data compression

  In practical work, Hive data processing which generally require compressed, for example, compressed data generated or map-side compression also reduce side before outputting the data generated. This saves our network bandwidth.

  1.1 MR supported compression coding

    

 

 

    To support a variety of compression / decompression algorithms, the Hadoop introduced encoder / decoder, as shown in the following table:

  

 

 

   Compression performance comparison:

  

   Snappy and compression efficiency is highest: official website, I read, but I have not been tested.

  On a single core of a Core i7 processor in 64-bit mode, Snappy compresses at about 250 MB/sec or more and decompresses at about 500 MB/sec or more.

  1.2 press configuration parameters

  

  1.3 output stage compression turned on Map

   Open the output stage of the compression map job transfer amount between the data map and Reduce task can be reduced. Specific configuration is as follows: Note that these configurations can be arranged directly in the hive-site.xml and mapred-site.xml configuration file.

  Examples of operation:

   1) Open the intermediate transfer data compression hive

set hive.exec.compress.intermediate=true;

  2) open in the map output compression mapreduce

set mapreduce.map.output.compress=true;

  3)设置mapreduce中map输出数据的压缩方式

set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;

  4)执行查询语句

select count(1) from score;

  1.4 开启Reduce输出阶段压缩

  当Hive将输出写入到表中时,输出内容同样可以进行压缩。属性hive.exec.compress.output控制着这个功能。用户可能需要保持默认设置文件中的默认值false,这样默认的输出就是非压缩的纯文本文件了。用户可以通过在查询语句或执行脚本中设置这个值为true,来开启输出结果压缩功能。

  实际操作:

  1)开启hive最终输出数据压缩功能

set hive.exec.compress.output=true // 默认值是false 可以直接set hive.exec.comperss.output;查看

  2)开启mapreduce最终输出数据压缩

set mapreduce.output.fileoutputformat.compress=true;

  3)设置mapreduce最终数据输出压缩方式

set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;

  4)设置mapreduce最终数据输出压缩为块压缩

set mapreduce.output.fileoutputformat.compress.type=BLOCK;

  5)测试一下输出结果是否是压缩文件

insert overwrite local directory '/export/servers/snappy' select * from score distribute by s_id sort by s_id desc;

  可以在snappy中找到一个文件是压缩过后的文件,文件打开是一系列的字节码。

二,Hive的数据存储格式

  Hive支持的存储数的格式主要有:TEXTFILE(行式存储) 、SEQUENCEFILE(行式存储)ORC(列式存储)、PARQUET(列式存储)。 

  2.1 列式存储和行式存储

  

  上图左边为逻辑表,右边第一个为行式存储,第二个为列式存储。

  行存储的特点: 查询满足条件的一整行数据的时候,列存储则需要去每个聚集的字段找到对应的每个列的值,行存储只需要找到其中一个值,其余的值都在相邻地方,所以此时行存储查询的速度更快。

  列存储的特点: 因为每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的数据量;每个字段的数据类型一定是相同的,列式存储可以针对性的设计更好的设计压缩算法。

TEXTFILE和SEQUENCEFILE的存储格式都是基于行存储的;
ORC和PARQUET是基于列式存储的

  2.2 常用的数据存储格式

  TEXTFILE格式

  默认格式,数据不做压缩,磁盘开销大,数据解析开销大。可结合Gzip、Bzip2使用.

  ORC格式

  Orc (Optimized Row Columnar)是hive 0.11版里引入的新的存储格式。如下图:

  

 

  可以看到每个Orc文件由1个或多个stripe组成,每个stripe250MB大小,每个Stripe里有三部分组成,分别是Index Data,Row Data,Stripe Footer:

[indexData]():某些列的索引数据
[rowData]() :真正的数据存储
[StripFooter]():stripe的元数据信息

  PARQUET格式

  Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,Parquet文件是以二进制方式存储的,所以是不可以直接读取的,文件中包括该文件的数据和元数据,因此Parquet格式文件是自解析的。

  通常情况下,在存储Parquet数据的时候会按照Block大小设置行组的大小,由于一般情况下每一个Mapper任务处理数据的最小单位是一个Block,这样可以把每一个行组由一个Mapper任务处理,增大任务执行并行度。Parquet文件的格式如下图所示。

  

三,文件存储格式和压缩的结合

  3.1 压缩比和查询速度对比

  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)向表中加载数据

load data local inpath '/export/servers/hivedatas/log.data' into table log_text ;

  (3)查看表中数据大小

  源文件大小:

  

   上传到hdfs表目录下的大小:

  

  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 ; // 指定为orc

  (2)向表中加载数据

insert into table log_orc select * from log_text ;

  (3)查看表中数据大小

   这里就很明显的看出来了ORC进行比较大的压缩。我这里就不一一列举,其他可自己尝试。

  压缩结论:压缩比

  ORC > Parquet > textFile

  4)存储文件的查询速度测试:

1)TextFile

hive (default)> select count(*) from log_text;

Time taken: 21.54 seconds, Fetched: 1 row(s)

2)ORC

hive (default)> select count(*) from log_orc;

Time taken: 20.867 seconds, Fetched: 1 row(s)

3)Parquet

hive (default)> select count(*) from log_parquet;

Time taken: 22.922 seconds, Fetched: 1 row(s)

  结论:

  存储文件的查询速度总结:

  ORC > TextFile > Parquet

  3.2 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");

  压缩后再看数据的大小:比没压缩的大,是因为这里是小文件,所以在小文件不要进行压缩。

  

   在实际的项目开发当中,hive表的数据存储格式一般选择:orc或parquet。压缩方式一般选择snappy

  

Guess you like

Origin www.cnblogs.com/tashanzhishi/p/12064732.html