大数据学习(五):在MapReduce中使用压缩

要想压缩MapReduec作业的输出,应在作业配置过程中将mapreduce.output.fileOutputformat.compress属性设置为true,将,apre-duce.output.fileoutputformat.compress.codec属性设置为打算压缩的压缩coec的类名。

另一种方案是在FileOutputFormat中使用更便捷的方法设置这些属性。

package cn.weida.hadoop.read;

import java.io.IOException;
import java.security.AlgorithmParameterGeneratorSpi;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class MaxTemperaturewithCompression {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        if (args.length!=2) {
            System.err.println("Usage: MaxTemperature <input path> <output path");
            System.exit(-1);
        }
        Job job = new Job();
        job.setJarByClass(MaxTemperature.class);      //传递一个类名。利用类名寻找jar执行包
        job.setJobName("Max temperature");
        
        FileInputFormat.addInputPath(job, new Path(args[0]));   //定义输入数据路径
        FileOutputFormat.setOutputPath(job, new Path(args[1])); //定义输出数据路径
        
        job.setMapperClass(MaxTemperatureMapper01.class);    //指定map函数和reduce函数的类
        job.setReducerClass(MaxTemperatureReduce01.class);
        job.setCombinerClass(MaxTemperatureReduce01.class);
        
        FileOutputFormat.setCompressOutput(job, true);               //设置可压缩
        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);  //设置压缩格式

        
        job.setOutputKeyClass(Text.class);              //设置输出键 值的类型  : 与reduce 函数的类型一致
        job.setOutputKeyClass(IntWritable.class);
        
        System.exit(job.waitForCompletion(true)? 0 : 1);
    }

尽管MapReduce应用读/写的是未经压缩的数据,但是如果对map阶段的中间的输入进行压缩,减少输出需要写到磁盘并通过网络传输到Reduce节点的开销

Configuration conf = new Configuration()l

conf.setBoolean(job.MAP_OUTPUT_COMRESS,true)

conf.setClass(Job.MAP_OUTPUT_cOMPRESS_CODEC,GzipCodec.class,CompressionCodec.class);

Job job = new Job(conf)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值