hadoop入门 使用MapReduce编写comibiner WordCount程序(五)

1、为什么要用combiner
常,一个map可能会产生大量的输出,combiner的作用就是在map端对输出先做一次合并,以减少传输到reducer的数据量
2、mapper

package com.example.hadoop.combiner;

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> {
    private Text outT=new Text();
    private IntWritable outV=new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1、获取一行
        String line=value.toString();
        //2、切割
        String[] words=line.split(" ");
        //3、循环写出
        for (String word : words) {
            //封装outK
            outT.set(word);
            //写出
            context.write(outT,outV);
        }
    }
}

3、reducer

package com.example.hadoop.combiner;

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> {
    private IntWritable outV=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();
        }
        outV.set(sum);
        //写出
        context.write(key,outV);
    }
}

4、combiner
其实代码逻辑和reducer是一样的,可以用同个类,但是这里为了清晰分开写,其实就是在map端先执行了以下reducer的逻辑

package com.example.hadoop.combiner;

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

import java.io.IOException;

public class WorderCountCombiner extends Reducer<Text, IntWritable,Text,IntWritable>{
    private IntWritable outV=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();
        }
        outV.set(sum);
        //写出
        context.write(key,outV);
    }
}

5、main方法
主要代码job.setCombinerClass(WorderCountCombiner.class);

package com.example.hadoop.combiner;


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, InterruptedException, ClassNotFoundException {
        //1、获取job
        Configuration conf = new Configuration();
        Job job= Job.getInstance(conf);
        //2、设置jar包途径
        job.setJarByClass(WordCountDriver.class);
        //3、关联mapper和reducer
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //4、设置map输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5、设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //设置combiner
        job.setCombinerClass(WorderCountCombiner.class);
        //6、设置输入路径和输出路径
        FileInputFormat.setInputPaths(job,new Path("D:\\cls.txt"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\hadoop\\output"));
        //7、提交job
        boolean result=job.waitForCompletion(true);

        System.exit(result?0:1);
    }
}

6、测试数据

sprak sprak hive hadoop netty sprak sprak hive hadoop netty sprak hive hadoop netty hive hadoop netty
sprak sprak hive hadoop netty sprak sprak hive hadoop netty sprak hive hadoop netty hive hadoop netty
sprak sprak hive hadoop netty sprak sprak hive hadoop netty sprak hive hadoop netty hive hadoop netty

结果
在这里插入图片描述
7、debug过程
先执行combiner
在这里插入图片描述
再执行reducer
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值