hive 压缩格式汇总

3 篇文章 0 订阅
2 篇文章 0 订阅

      今天,看了几篇hive压缩格式相关的博客觉得不错,在这里做一下提取要点汇总。

0 前言

数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能。

     而hive中间结果是map输出传给reduce,所以应该使用低cpu开销和高压缩效率,一般最好使用snappy

1 源数据在云上(hdfs)压缩存储

    Hadoop默认支持Gzip和BZip2的解压缩方式,可直接读取(hadoop fs -text命令),但hive只能用TEXTFILE格式的表加载,然后再insertoverwrite 到其他格式的表(比如SEQUENCEFILE表),如果hive其他格式的表想要直接加载压缩格式数据,需要重写INPUTFORMAT和OUTPUTFORMAT文件类

2 压缩格式文件的切分(不支持则hadoop不能并行的进行map操作)

    BZip2和LZO(提供block级的压缩)支持文件切分

    Gzip和Snappy则不支持。 

 mapreduce 的压缩

mapreduce 压缩 主要是在shuffle阶段的优化。 shuffle 端的 --partition (分区) -- sort (排序) -- combine (合并) -- compress (压缩) -- group (分组) 在mapreduce 优化shuffle 从本质上是解决磁盘的IO 与网络IO 问题。减少 集群件的文件传输处理。

4 hive 的压缩:

压缩的和解压需要cpu的,hive 的常见的压缩格式: bzip2,gzip,lzo,snappy等 cdh 默认采用的压缩是snappy 压缩比:bzip2 > gzip > lzo bzip2 最节省存储空间。 注意: sanppy 的并不是压缩比最好的 解压速度: lzo > gzip > bzip2 lzo 解压速度是最快的。 注意:追求压缩速率最快的sanppy 压缩的和解压需要cpu 损耗比较大。 集群分: cpu 的密集型 (通常是计算型的网络) hadoop 是 磁盘 IO 和 网络IO 的密集型, 网卡的双网卡绑定。

5 mapreduce 支持的压缩:

CodeName: zlib : org.apache.hadoop.io.compress.DefaultCodec gzip : org.apache.hadoop.io.compress.GzipCodec gzip2: org.apache.hadoop.io.compress.Bzip2Codec lzo : org.apache.hadoop.io.compress.LzoCodec lz4 : org.apache.hadoop.io.compress.Lz4Codec snappy: org.apache.hadoop.io.compress.SnappyCodec

 

6 行存储与列式存储区别

数据库列存储不同于传统的关系型数据库,其数据在表中是按行存储的,列方式所带来的重要好处之一就是,由于查询中的选择规则是通过列来定义的,因 此整个数据库是自动索引化的。   按列存储每个字段的数据聚集存储,在查询只需要少数几个字段的时候,能大大减少读取的数据量,一个字段的数据聚集存储,那就 更容易为这种聚集存储设计更好的压缩/解压算法。

TextFile:默认的类型,行存储 rcfile:按行块,每块再按列存储 avro:二进制
7 ORC格式

 

8  PARQUET格式

 

9 在表结构上指定文件类型

1) orc 类型

create table page_views_orc( track_time string, url string, session_id string, refere string, ip string, end_user_id string, city_id string ) row format delimited fields terminated by '\t' STORED AS orc ;

2) parquet类型

create table page_views_parquet( track_time string, url string, session_id string, refere string, ip string, end_user_id string, city_id string ) row format delimited fields terminated by '\t' STORED AS parquet ;

 

 

10 创建表与指定压缩

1)orc+snappy 格式

create table page_views_orc_snappy( track_time string, url string, session_id string, refere string, ip string, end_user_id string, city_id string ) row format delimited fields terminated by '\t' STORED AS orc TBLPROPERTIES("orc.compression"="Snappy");

2)parquet+snappy 格式

set parquet.compression=Snappy ; set hive.exec.compress.output=true ; create table page_views_parquet_snappy( track_time string, url string, session_id string, refere string, ip string, end_user_id string, city_id string ) row format delimited fields terminated by '\t' STORED AS parquet ;

对比:orc+snappy 存储和查询 > parquet + snappy

11 压缩格式 创建表文件格式 + 压缩参数

1)默认 text+default

--设置压缩类型为Gzip压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.DefaultCodec;  

2)gzip 格式 text+gzip

--设置压缩类型为Gzip压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 

文件.gz

3) Bzip2格式 text +bzip2

--设置压缩类型为Bzip2压缩: 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.BZip2Codec; 

文件.bz2

4) lzo 格式 text + lzo

--设置为LZO压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec; 

文件.lzo

5) snappy 格式 text + snappy

--设置压缩 

SET hive.exec.compress.output=true; 

SET mapred.compress.map.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression=org.apache.hadoop.io.compress.SnappyCodec; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec; 

SET io.compression.codecs=org.apache.hadoop.io.compress.SnappyCodec; 

文件.snappy

6)sequence 格式 sequence + default

--设置压缩类型为Gzip压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.DefaultCodec;  

文件如:000000_0 

7) sequence 格式 sequence + gzip

--设置压缩类型为Gzip压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 

文件:是一个密文的文件,无法通过gzip解压,如:000000_0 

8)rcfile 格式 rcfile + gzip

--设置压缩类型为Gzip压缩 

SET hive.exec.compress.output=true; 

SET mapred.output.compress=true; 

SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 

9) orcfile 格式

ORCFile有自己的参数设置压缩格式,一般不使用上述Hive参数设置压缩参数。

a orcfile + zlib

CREATE TABLE student_orcfile_zlib (id STRING, name STRING) 

ROW FORMAT DELIMITED 

    FIELDS TERMINATED BY ',' 

    LINES TERMINATED BY '\n' 

STORED AS ORCFILE TBLPROPERTIES ("orc.compress"="ZLIB"); 

b orcfile+snappy

CREATE TABLE student_orcfile_snappy2 (id STRING, name STRING) 

ROW FORMAT DELIMITED 

 FIELDS TERMINATED BY ',' 

 LINES TERMINATED BY '\n' 

STORED AS ORCFILE TBLPROPERTIES ("orc.compress"="SNAPPY"); 

参考博客:

https://blog.csdn.net/longshenlmj/article/details/50550580

https://blog.51cto.com/flyfish225/2097274

http://bigdata.51cto.com/art/201706/542623.htm

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值