Hadoop编码/解码器方式,以下表所示算法
压缩格式 | 对应的编码/解码 |
---|---|
DEFAULT | org.apache.hadoop.io.compress.DefaultCodec |
Gzip | org.apache.hadoop.io.compress.GzipCodec |
Bzip | org.apache.hadoop.io.compress.BzipCodec |
Snappy | org.apache.hadoop.io.compress.SnappyCodec |
Lzo | org.apache.hadoop.io.compress.LzopCodec |
HiveQL语句最终都将转换成为hadoop中的MapReduce job,而MapReduce job能够有对处理的数据进行压缩。apache
Hive中间数据压缩api
hive.exec.compress.intermediate:默认为false,设置true为激活中间数据压缩功能,就是MapReduce的shuffle阶段对mapper产生中间压缩,在这个阶段,优先选择一个低CPU开销:并发
set hive.exec.compress.intermediate=true set mapred.map.output.compression.codec= org.apache.hadoop.io.compress.SnappyCodec set mapred.map.output.compression.codec=com.hadoop.compression.lzo.LzoCodec
最终输出结果压缩
hive.exec.compress.output:用户能够对最终生成的Hive表的数据一般也须要压缩。该参数控制这一功能的激活与禁用,设置为true来声明将结果文件进行压缩。app
mapred.output.compression.codec:将hive.exec.compress.output参数设置成true后,而后选择一个合适的编解码器,如选择SnappyCodec。设置以下(两种压缩的编写方式是同样的):工具
set hive.exec.compress.output=true set mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec 或者 或者 set mapred.output.compress=true set mapred.output.compression.codec=org.apache.hadoop.io.compress.LzopCodec
1. TEXTFILEoop
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; 插入数据操做: set hive.exec.compress.output=true; //输出结果压缩开启 set mapred.output.compress=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; //压缩和解压缩编码类列表,用逗号分隔,将所用到解压和压缩码设置其中 insert overwrite table textfile_table select * from testfile_table;
2. SEQUENCEFILE性能
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; 插入数据操做: set hive.exec.compress.output=true; set mapred.output.compress=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; SET mapred.output.compression.type=BLOCK; insert overwrite table seqfile_table select * from testfile_table;
3. 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; 插入数据操做: set hive.exec.compress.output=true; set mapred.output.compress=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; insert overwrite table rcfile_table select * from testfile_table;
4. ORCFILE编码
create table if not exists orcfile_table( site string, url string, pv bigint, label string) row format delimited fields terminated by '\t' stored as orc; 插入数据操做: set hive.exec.compress.output=true; set mapred.output.compress=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec; insert overwrite table orcfile_table select * from testfile_table;
总结:
TextFile默认格式,加载速度最快,能够采用Gzip进行压缩,压缩后的文件没法split,没法并行处理了。
SequenceFile压缩率最低,查询速度通常,将数据存放到sequenceFile格式的hive表中,这时数据就会压缩存储。三种压缩格式NONE,RECORD,BLOCK。是可分割的文件格式。
相比TEXTFILE和SEQUENCEFILE,RCFILE因为列式存储方式,数据加载时性能消耗较大,可是具备较好的压缩比和查询响应。数据仓库的特色是一次写入、屡次读取,所以,总体来看,RCFILE相比其他两种格式具备较明显的优点。
在hive中使用压缩须要灵活的方式,若是是数据源的话,采用RCFile+bz或RCFile+gz的方式,这样能够很大程度上节省磁盘空间;而在计算的过程当中,为了避免影响执行的速度,能够浪费一点磁盘空间,建议采用RCFile+snappy的方式,这样能够总体提高hive的执行速度。至于lzo的方式,也能够在计算过程当中使用,只不过综合考虑(速度和压缩比)仍是考虑snappy适宜。