maprecude-combiner合并及实例

看这

combinner是mapreduce程序中为了减小网络传输量而设置的一个组件,其父类其实就是reducer,combinner在每一个maptask后面进行数据的汇总,从而减少在map之后reduce之前的shuffle机制一系列的数据读写的io操作。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NpXpolg1-1603713044018)(https://s1.ax1x.com/2020/10/26/BuxnI0.png)]

注意能够应用的前提不要影响到业务逻辑,combinner只是一种对mapreduce程序的优化手段,适用于对数据进行汇总的mr程序,不适用于对数据进行求平均的mr程序。

实例 word count + combinner

创建一个combinner类继承Reducer,并在驱动类中将mr程序与其关联。

若reduce与combinner进行的操作一致,那么直接设置reducer为combinner类也无妨。

combinner&reducer

package com.wordcount.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class wordcountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    //Text k = new Text();
    IntWritable v = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable value:values) {
            sum +=value.get();
        }
        v.set(sum);
        context.write(key,v);

    }
}

mapper

package com.wordcount.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class wordcountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    Text k = new Text();
    IntWritable v = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] fields = value.toString().split("\t");

        for (String fied:fields) {
            k.set(fied);
            context.write(k,v);
        }
    }
}

driver

package com.wordcount.mr;

//import javax.security.auth.login.AppConfigurationEntry;
//import javax.security.auth.login.Configuration;

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

import java.io.IOException;

public class wordcountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"D:/mapreduceinput/input2","D:/mapreduceoutput/output4"};
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setJarByClass(wordcountDriver.class);
        job.setMapperClass(wordcountMapper.class);
        job.setReducerClass(wordcountReducer.class);

        job.setCombinerClass(wordcountReducer.class);//注意这里直接将reducer设置为了combinner

        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值