MapReduce框架-combiner

Combiner

1.对combiner的理解

combiner其实属于优化方案,由于带宽限制,应该尽量map和reduce之间的数据传输数量。它在Map端把同一个key的键值对合并在一起并计算,计算规则与reduce一致,所以combiner也可以看作特殊的Reducer。
执行combiner操作要求开发者必须在程序中设置了combiner(程序中通过job.setCombinerClass(myCombine.class)自定义combiner操作)

2.哪里使用combiner?

1,map输出数据根据分区排序完成后,在写入文件之前会执行一次combine操作(前提是作业中设置了这个操作);
2,如果map输出比较大,溢出文件个数大于3(此值可以通过属性min.num.spills.for.combine配置)时,在merge的过程(多个spill文件合并为一个大文件)中前还会执行combiner操作;

3.注意事项

不是每种作业都可以做combiner操作的,只有满足以下条件才可以:
1,combiner只应该用于那种Reduce的输入key/value与输出key/value类型完全一致,因为combine本质上就是reduce操作。
2,计算逻辑上,combine操作后不会影响计算结果,像求和,最大值就不会影响,求平均值就影响了。

4.wordcount的cominer开发实例

package com.sl.bigdatatest.mapreduce;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

    public static void main(String[] args) throws Exception {

        if (args.length < 2) {
            System.err.println("Uage:<in> <out>");
            System.exit(2);
        }

        String inputPath = args[0];
        Path outputPath = new Path(args[1]);

        //1.configuration
        Configuration conf = new Configuration();
        URI uri = new URI("hdfs://192.168.0.200:9000");
        FileSystem fileSystem = FileSystem.get(uri, conf);

        if (fileSystem.exists(outputPath)) {
            boolean b = fileSystem.delete(outputPath, true);
            System.out.println("已存在目录删除:"+b);
        }

        //2.建立job
        Job job = Job.getInstance(conf, WordCount.class.getName());
        job.setJarByClass(WordCount.class);

        //3.输入文件
        FileInputFormat.setInputPaths(job, new Path(inputPath));

        //4.格式化输入文件
        job.setInputFormatClass(TextInputFormat.class);

        //5.map
        job.setMapperClass(MapWordCountTask.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //6.reduce
        job.setReducerClass(ReduceWordCountTask.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        /**指定本job使用combiner组件,组件所用的类为ReduceWordCountTask**/
        job.setCombinerClass(ReduceWordCountTask.class);

        //7.输出文件
        FileOutputFormat.setOutputPath(job, outputPath);

        //8.输出文件格式化
        job.setOutputFormatClass(TextOutputFormat.class);

        //9.提交给集群执行
        job.waitForCompletion(true);

    }

    public static class MapWordCountTask extends Mapper<LongWritable, Text, Text, LongWritable> {

        private Text k2 = new Text();
        private LongWritable v2 = new LongWritable();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws Exception {
            String content = value.toString();
            StringTokenizer st = new StringTokenizer(content);
            while (st.hasMoreElements()) {
                k2.set(st.nextToken());
                v2.set(1L);
                context.write(k2, v2);
            }
        }
    }

    public static class ReduceWordCountTask extends Reducer<Text, LongWritable, Text, LongWritable> {

        private LongWritable v3 = new LongWritable();

        @Override
        protected void reduce(Text k2, Iterable<LongWritable> v2s,Context context) throws Exception {
            long sum = 0;
            for (LongWritable longWritable : v2s) {
                sum += longWritable.get();
                v3.set(sum);
            }
            context.write(k2, v3);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值