深入探究如何使用Java编写MapReduce程序
作者:上进小菜猪 发布时间:2022-10-17 18:40:04
MapReduce的原理
MapReduce由两个主要阶段组成:Map和Reduce。在Map阶段中,数据集被分成若干个小块,每个小块由Map函数处理,输出一系列键值对。在Reduce阶段中,键值对被聚合成一组较小的结果集。下面我们详细讲解每个阶段的原理。
Map阶段
Map阶段的输入是原始数据集。它将输入数据划分成若干个小块,每个小块由Map函数处理。Map函数的输入是键值对,输出也是键值对。在Map函数中,对每个输入键值对进行操作,生成一组中间键值对,这些中间键值对将作为Reduce阶段的输入。
Reduce阶段
Reduce阶段的输入是Map阶段输出的中间键值对集合。Reduce函数对每个键执行聚合操作,并将结果输出到最终结果集。Reduce函数的输出通常是单个键值对,但也可以是多个键值对。
Shuffle阶段
Shuffle阶段在Map和Reduce阶段之间执行。在Map阶段中,每个Map任务都会生成一组中间键值对。在Shuffle阶段中,这些中间键值对将按照键进行排序并分组,以便Reduce任务可以并行处理具有相同键的中间结果。
MapReduce程序实现
下面我们将使用Java编写一个简单的MapReduce程序。这个程序将计算输入文本中每个单词的出现次数。
首先,我们需要编写Map函数。Map函数将输入文本中的每个单词映射为一个键值对,其中键是单词本身,值是1。以下是Map函数的代码:
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
接下来,我们编写Reduce函数。Reduce函数将具有相同键的值相加,并将结果作为键值对输出。以下是Reduce函数的代码:
javaCopy codepublic static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(key, new IntWritable(sum));
最后,我们将Map函数和Reduce函数组合起来,并将它们作为MapReduce程序的一部分提交给Hadoop集群。以下是完整的MapReduce程序:
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
public class WordCount {
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "wordcount");
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
在上面的代码中,我们首先定义了Map类和Reduce类,然后在main函数中将它们组合起来,使用Job类将程序提交给Hadoop集群进行处理。我们使用FileInputFormat和FileOutputFormat指定输入和输出路径。
来源:https://juejin.cn/post/7231200657900847141


猜你喜欢
- 目录概述准备工作使用概述springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式
- 一、单向通信功能:客户端发送一句话到服务器:客户端:public class TestClient {//客户端
- 泛型泛型的语法定义class 类名 <泛型标识,泛型标识,…>{ private 泛型标识1,变量名;常用
- 本文实例讲述了Android滑动按钮事件。分享给大家供大家参考,具体如下:今天纪录一下滑动按钮功能。。首先效果图:然后是分别建立三个文件,第
- 一、问题来源项目中遇到 json 模型映射成 RadialGradient 组件的需求,其他参数正常传递即可;唯独 radius 参数效果有
- 写在前面关于数据结构,Java官方其实已经帮我们写好并封装起来了,在真正需要使用的时候直接调用即可,但为了更好的理解数据结构,我会按照源码的
- 做个网站的安卓客户端,用户安装到自己手机上,如果我出了新版本怎么办呢?要有版本更新功能。 本来版本检测最好可以自动进行。但如果每次开启程序,
- 本文实例为大家分享了java实现猜数字小游戏的具体代码,供大家参考,具体内容如下介绍:电脑随机生成一个1-100的随机数,用户输入一个数;电
- AbstractQueuedSynchronizerAbstractQueuedSynchronizer 简称 AQS ,抽象队列同步器,用
- 任何简单操作的背后,都有一套相当复杂的机制。本文将以SpringBoot应用的在Docker环境下的打包部署为例,详细讲解如何使用Jenki
- 自定义TypeHandler映射JSON类型为List1. 实体类这里只展示需要映射的字段,分别在所需映射的字段和实体类上添加注解。&nbs
- 一、检查数组是否包含某个值的方法使用Listpublic static boolean useList(String[] arr, Stri
- 1. Resource Basics(1) Manifest Resources(资源清单)资源在编译期间添加到程序集。如果要将资源嵌入到程
- 一般表单数据分为两类<form method="post" action="${pageContext.
- 前言本章是关于Java数组的最全汇总,本篇为汇总中篇,主要讲了二维数组和不规则的数组的相关内容。数组是最常见的一种数据结构,它是相同类型的用
- 简介 Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的。它
- 通过MAVEN完成 Mybatis 逆向工程1. POM文件中添加插件在 pom 文件的build 标签中 添加 plugin 插件和 数据
- 《Android开发艺术探索》笔记:在Activity的onCreate()或者onResume()中去获得View的高度的时候不能正确获得
- 先看实例 代码如下 classip { privatestaticlongiptolong(stringstrip) //将127.0.0.
- 本文实例讲述了Android popupWindow弹出窗体实现方法。分享给大家供大家参考,具体如下:1. 建立popupwindow显示的