WordCount 源码解析 Mapper,Reducer,Driver

创建包 com.nefu.mapreduce.wordcount ,开始编写 Mapper , Reducer ,
Driver

用户编写的程序分成三个部分: Mapper 、 Reducer 和 Driver 。
( 1 ) Mapper 阶段
➢ 用户自定义的 Mapper 要继承自己的父类
➢ Mapper 的输入数据是 KV 对的形式 (KV 的类型可自定义 )
➢ Mapper 中的业务逻辑写在 map () 方法中
➢ Mapper 的输出数据是 KV 对的形式 (KV 的类型可自定义 )
➢ map () 方法 (MapTask 进程 ) 对每一个 <K.V> 调用一次

java 复制代码
package com.nefu.mapreducer.wordcount;

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

( 2 ) Reducer 阶段
➢ 用户自定义的 Reducer 要继承自己的父类
➢ Reducer 的输入数据类型对应 Mapper 的输出数据类型,也是 KV
➢ Reducer 的业务逻辑写在 reduce() 方法中
➢ ReduceTask 进程对每一组相同 k 的 <k,v> 组调用一 次 reduce () 方法,迭代
器类型

java 复制代码
package com.nefu.mapreducer.wordcount;

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=sum+value.get();
        }
        outV.set(sum);
        context.write(key,outV);
    }
}

( 3 ) Driver 阶段
相当于 YARN 集群的客户端,用于提交我们整个程序到 YARN 集群,提交的是
封装了 MapReduce 程序相关运行参数的 job 对象

java 复制代码
package com.nefu.mapreducer.wordcount;

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 InterruptedException, IOException, ClassNotFoundException {
        //获取job
        Configuration conf=new Configuration();
        Job job=Job.getInstance(conf);
        //设置jar包
        job.setJarByClass(WordcountDriver.class);

        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReducer.class);

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

        FileInputFormat.setInputPaths(job,new Path("D:\\cluster\\mapreduce.txt"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\cluster\\partion"));
        boolean result=job.waitForCompletion(true);
        System.exit(result?0:1);
    }
}
复制代码
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>


相关推荐
猴哥源码8 分钟前
基于Java+SpringBoot的动物领养平台
java·spring boot
老任与码12 分钟前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
小兵张健13 分钟前
武汉拿下 23k offer 经历
java·面试·ai编程
FreeBuf_23 分钟前
Apache组件遭大规模攻击:Tomcat与Camel高危RCE漏洞引发数千次利用尝试
java·tomcat·apache
nananaij30 分钟前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
无妄-202434 分钟前
软件架构升级中的“隐形地雷”:版本选型与依赖链风险
java·服务器·网络·经验分享
qqxhb38 分钟前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
阿蒙Amon40 分钟前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#
无小道41 分钟前
c++-引用(包括完美转发,移动构造,万能引用)
c语言·开发语言·汇编·c++
爱莉希雅&&&1 小时前
技术面试题,HR面试题
开发语言·学习·面试