【Hive】hive的数据压缩


Hive文件存储格式包括以下几类:
1、TEXTFILE
2、SEQUENCEFILE
3、RCFILE
4、ORCFILE
  其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理。
  SEQUENCEFILE,RCFILE,ORCFILE格式的表不能直接从本地文件导入数据,数据要先导入到textfile格式的表中, 然后再从表中用insert导入SequenceFile,RCFile,ORCFile表中。

 

更多用法,一定要去看官网啊!!! 
  https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

 

 

一、TEXTFILE 格式
  默认格式,数据不做压缩,磁盘开销大,数据解析开销大。 可结合Gzip、Bzip2使用(系统自动检查,执行查询时自动解压),但使用这种方式,Hive不会对数据进行切分, 从而无法对数据进行并行操作。

  

  示例:

复制代码
create table if not exists textfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited fields terminated by '\t'
stored as textfile;
复制代码

 


  插入数据操作:

Hive> Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> insert overwrite table textfile_table select * from textfile_table;

 

 

 


二、SEQUENCEFILE 格式
SequenceFile是Hadoop API提供的一种二进制文件支持,其具有使用方便、可分割、可压缩的特点。 
SequenceFile支持三种压缩选择:NONE,RECORD,BLOCK。Record压缩率低,一般建议使用BLOCK压缩。
  示例:

复制代码
create table if not exists seqfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited
fields terminated by '\t'
stored as sequencefile;
复制代码

 


  插入数据操作:

Hive> set Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> SET mapred.output.compression.type=BLOCK;
Hive> insert overwrite table seqfile_table select * from textfile_table;

 

 

 


三、RCFILE 文件格式
RCFILE是一种行列存储相结合的存储方式。首先,其将数据按行分块,保证同一个record在一个块上,避免读一个记录需要读取多个block。
其次,块数据列式存储,有利于数据压缩和快速的列存取。
  RCFILE文件示例:

复制代码
create table if not exists rcfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited
fields terminated by '\t'
stored as rcfile;
复制代码

 


  插入数据操作:

Hive> set Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> insert overwrite table rcfile_table select * from textfile_table;

 

 

 


四、ORCFILE()
  以后补充

 

 


五、再看TEXTFILE、SEQUENCEFILE、RCFILE三种文件的存储情况:
[hadoop@master ~]$ hadoop dfs -dus /user/Hive/warehouse/*
hdfs://master :9000/user/Hive/warehouse/hbase_table_1 0
hdfs://master :9000/user/Hive/warehouse/hbase_table_2 0
hdfs://master :9000/user/Hive/warehouse/orcfile_table 0
hdfs://master :9000/user/Hive/warehouse/rcfile_table 102638073
hdfs://master :9000/user/Hive/warehouse/seqfile_table 112497695
hdfs://master :9000/user/Hive/warehouse/testfile_table 536799616
hdfs://master :9000/user/Hive/warehouse/textfile_table 107308067
[hadoop@singlehadoop ~]$ hadoop dfs -ls /user/Hive/warehouse/*/
-rw-r--r-- 2 hadoop supergroup 51328177 2014-03-20 00:42 /user/Hive/warehouse/rcfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 51309896 2014-03-20 00:43 /user/Hive/warehouse/rcfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 56263711 2014-03-20 01:20 /user/Hive/warehouse/seqfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 56233984 2014-03-20 01:21 /user/Hive/warehouse/seqfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 536799616 2014-03-19 23:15 /user/Hive/warehouse/testfile_table/weibo.txt
-rw-r--r-- 2 hadoop supergroup 53659758 2014-03-19 23:24 /user/Hive/warehouse/textfile_table/000000_0.gz
-rw-r--r-- 2 hadoop supergroup 53648309 2014-03-19 23:26 /user/Hive/warehouse/textfile_table/000001_1.gz


  总结: 相比TEXTFILE和SEQUENCEFILE,RCFILE由于列式存储方式,数据加载时性能消耗较大,但是具有较好的压缩比和查询响应。
数据仓库的特点是一次写入、多次读取,因此,整体来看,RCFILE相比其余两种格式具有较明显的优势。

 

 

 

以下,本文转自于。http://blog.csdn.net/cnbird2008/article/details/9182869

Hive数据压缩

本文介绍Hadoop系统中Hive数据压缩方案的比较结果及具体压缩方法。

一、压缩方案比较

关于Hadoop HDFS文件的压缩格式选择,我们通过多个真实的Track数据做测试,得出结论如下:

1.  系统的默认压缩编码方式 DefaultCodec 无论在压缩性能上还是压缩比上,都优于GZIP 压缩编码。这一点与网上的一些观点不大一致,网上不少人认为GZIP的压缩比要高一些,估计和Cloudera的封装及我们Track的数据类型有关。

2.  Hive文件的RCFile 的在压缩比,压缩效率,及查询效率上都优于SEQENCE FILE (包括RECORD, BLOCK 级别) 。

3.  所有压缩文件均可以正常解压为TEXT 文件,但比原始文件略大,可能是行列重组造成的。

 

 

 

关于压缩文件对于其他组件是适用性如下:

1.  Pig 不支持任何形式的压缩文件。

2.  Impala 目前支持SequenceFile的压缩格式,但还不支持RCFile的压缩格式。

 

 

综上所述

  从压缩及查询的空间和时间性能上来说,DefaultCodeC + RCFile的压缩方式均为最优,但使用该方式,会使得Pig 和Impala 无法使用(Impala的不兼容不确定是否是暂时的)。

  而DefaultCodeC+ SequenceFile 在压缩比,查询性能上略差于RCFile (压缩比约 6:5), 但可以支持 Impala实时查询。

 

推荐方案

 采用RCFile 方式压缩历史数据。FackBook全部hive表都用RCFile存数据。

 

 

 

二、局部压缩方法

只需要两步:

1.      创建表时指定压缩方式,默认不压缩,以下为示例:

create external table track_hist(

id bigint, url string, referer string, keyword string, type int, gu_idstring,

…/*此处省略中间部分字段*/ …, string,ext_field10 string)

partitioned by (ds string) stored as RCFile location '/data/share/track_histk' ;

 

2.  插入数据是设定立即压缩

SET hive.exec.compress.output=true;

insert overwrite table track_histpartition(ds='2013-01-01')

select id,url, …/*此处省略中间部分字段*/ …, ext_field10 fromtrackinfo

where ds='2013-01-01';

 

 

 

 

 

三、全局方式,修改属性文件

在hive-site.xml中设置:

<property>

 <name>hive.default.fileformat</name>

 <value>RCFile</value>

 <description>Default file format for CREATE TABLE statement.Options are TextFile and SequenceFile. Users can explicitly say CREAT

E TABLE ... STORED AS&lt;TEXTFILE|SEQUENCEFILE&gt; to override</description>

</property>

<property>

 <name>hive.exec.compress.output</name>

 <value>true</value>

 <description> This controls whether the final outputs of a query(to a local/hdfs file or a hive table) is compressed. The compres

sion codec and other options are determinedfrom hadoop config variables mapred.output.compress* </description>

 

 

 

 

四、注意事项

1、Map阶段输出不进行压缩

2、对输出文本进行处理时不压缩

 

Hive文件存储格式包括以下几类:
1、TEXTFILE
2、SEQUENCEFILE
3、RCFILE
4、ORCFILE
  其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理。
  SEQUENCEFILE,RCFILE,ORCFILE格式的表不能直接从本地文件导入数据,数据要先导入到textfile格式的表中, 然后再从表中用insert导入SequenceFile,RCFile,ORCFile表中。

 

更多用法,一定要去看官网啊!!! 
  https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

 

 

一、TEXTFILE 格式
  默认格式,数据不做压缩,磁盘开销大,数据解析开销大。 可结合Gzip、Bzip2使用(系统自动检查,执行查询时自动解压),但使用这种方式,Hive不会对数据进行切分, 从而无法对数据进行并行操作。

  

  示例:

复制代码
create table if not exists textfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited fields terminated by '\t'
stored as textfile;
复制代码

 


  插入数据操作:

Hive> Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> insert overwrite table textfile_table select * from textfile_table;

 

 

 


二、SEQUENCEFILE 格式
SequenceFile是Hadoop API提供的一种二进制文件支持,其具有使用方便、可分割、可压缩的特点。 
SequenceFile支持三种压缩选择:NONE,RECORD,BLOCK。Record压缩率低,一般建议使用BLOCK压缩。
  示例:

复制代码
create table if not exists seqfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited
fields terminated by '\t'
stored as sequencefile;
复制代码

 


  插入数据操作:

Hive> set Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> SET mapred.output.compression.type=BLOCK;
Hive> insert overwrite table seqfile_table select * from textfile_table;

 

 

 


三、RCFILE 文件格式
RCFILE是一种行列存储相结合的存储方式。首先,其将数据按行分块,保证同一个record在一个块上,避免读一个记录需要读取多个block。
其次,块数据列式存储,有利于数据压缩和快速的列存取。
  RCFILE文件示例:

复制代码
create table if not exists rcfile_table(
site string,
url string,
pv bigint,
label string)
row format delimited
fields terminated by '\t'
stored as rcfile;
复制代码

 


  插入数据操作:

Hive> set Hive.exec.compress.output=true; 
Hive> set mapred.output.compress=true; 
Hive> set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; 
Hive> set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; 
Hive> insert overwrite table rcfile_table select * from textfile_table;

 

 

 


四、ORCFILE()
  以后补充

 

 


五、再看TEXTFILE、SEQUENCEFILE、RCFILE三种文件的存储情况:
[hadoop@master ~]$ hadoop dfs -dus /user/Hive/warehouse/*
hdfs://master :9000/user/Hive/warehouse/hbase_table_1 0
hdfs://master :9000/user/Hive/warehouse/hbase_table_2 0
hdfs://master :9000/user/Hive/warehouse/orcfile_table 0
hdfs://master :9000/user/Hive/warehouse/rcfile_table 102638073
hdfs://master :9000/user/Hive/warehouse/seqfile_table 112497695
hdfs://master :9000/user/Hive/warehouse/testfile_table 536799616
hdfs://master :9000/user/Hive/warehouse/textfile_table 107308067
[hadoop@singlehadoop ~]$ hadoop dfs -ls /user/Hive/warehouse/*/
-rw-r--r-- 2 hadoop supergroup 51328177 2014-03-20 00:42 /user/Hive/warehouse/rcfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 51309896 2014-03-20 00:43 /user/Hive/warehouse/rcfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 56263711 2014-03-20 01:20 /user/Hive/warehouse/seqfile_table/000000_0
-rw-r--r-- 2 hadoop supergroup 56233984 2014-03-20 01:21 /user/Hive/warehouse/seqfile_table/000001_0
-rw-r--r-- 2 hadoop supergroup 536799616 2014-03-19 23:15 /user/Hive/warehouse/testfile_table/weibo.txt
-rw-r--r-- 2 hadoop supergroup 53659758 2014-03-19 23:24 /user/Hive/warehouse/textfile_table/000000_0.gz
-rw-r--r-- 2 hadoop supergroup 53648309 2014-03-19 23:26 /user/Hive/warehouse/textfile_table/000001_1.gz


  总结: 相比TEXTFILE和SEQUENCEFILE,RCFILE由于列式存储方式,数据加载时性能消耗较大,但是具有较好的压缩比和查询响应。
数据仓库的特点是一次写入、多次读取,因此,整体来看,RCFILE相比其余两种格式具有较明显的优势。

 

 

 

以下,本文转自于。http://blog.csdn.net/cnbird2008/article/details/9182869

Hive数据压缩

本文介绍Hadoop系统中Hive数据压缩方案的比较结果及具体压缩方法。

一、压缩方案比较

关于Hadoop HDFS文件的压缩格式选择,我们通过多个真实的Track数据做测试,得出结论如下:

1.  系统的默认压缩编码方式 DefaultCodec 无论在压缩性能上还是压缩比上,都优于GZIP 压缩编码。这一点与网上的一些观点不大一致,网上不少人认为GZIP的压缩比要高一些,估计和Cloudera的封装及我们Track的数据类型有关。

2.  Hive文件的RCFile 的在压缩比,压缩效率,及查询效率上都优于SEQENCE FILE (包括RECORD, BLOCK 级别) 。

3.  所有压缩文件均可以正常解压为TEXT 文件,但比原始文件略大,可能是行列重组造成的。

 

 

 

关于压缩文件对于其他组件是适用性如下:

1.  Pig 不支持任何形式的压缩文件。

2.  Impala 目前支持SequenceFile的压缩格式,但还不支持RCFile的压缩格式。

 

 

综上所述

  从压缩及查询的空间和时间性能上来说,DefaultCodeC + RCFile的压缩方式均为最优,但使用该方式,会使得Pig 和Impala 无法使用(Impala的不兼容不确定是否是暂时的)。

  而DefaultCodeC+ SequenceFile 在压缩比,查询性能上略差于RCFile (压缩比约 6:5), 但可以支持 Impala实时查询。

 

推荐方案

 采用RCFile 方式压缩历史数据。FackBook全部hive表都用RCFile存数据。

 

 

 

二、局部压缩方法

只需要两步:

1.      创建表时指定压缩方式,默认不压缩,以下为示例:

create external table track_hist(

id bigint, url string, referer string, keyword string, type int, gu_idstring,

…/*此处省略中间部分字段*/ …, string,ext_field10 string)

partitioned by (ds string) stored as RCFile location '/data/share/track_histk' ;

 

2.  插入数据是设定立即压缩

SET hive.exec.compress.output=true;

insert overwrite table track_histpartition(ds='2013-01-01')

select id,url, …/*此处省略中间部分字段*/ …, ext_field10 fromtrackinfo

where ds='2013-01-01';

 

 

 

 

 

三、全局方式,修改属性文件

在hive-site.xml中设置:

<property>

 <name>hive.default.fileformat</name>

 <value>RCFile</value>

 <description>Default file format for CREATE TABLE statement.Options are TextFile and SequenceFile. Users can explicitly say CREAT

E TABLE ... STORED AS&lt;TEXTFILE|SEQUENCEFILE&gt; to override</description>

</property>

<property>

 <name>hive.exec.compress.output</name>

 <value>true</value>

 <description> This controls whether the final outputs of a query(to a local/hdfs file or a hive table) is compressed. The compres

sion codec and other options are determinedfrom hadoop config variables mapred.output.compress* </description>

 

 

 

 

四、注意事项

1、Map阶段输出不进行压缩

2、对输出文本进行处理时不压缩

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值