MapReduce快速入门系列(9) | Shuffle之Combiner合并

article/2025/10/8 8:25:30

Hello,大家好!博主上篇讲解了分区,这篇要讲的是合并操作。如何讲解这个章节呢?首先先对什么是合并进行解释,然后通过案例进行证明。

目录

  • 一. Combiner合并的简单介绍
  • 二. 通过图片了解使用Combiner和不使用的区别
  • 三. 代码实现
    • 3.1 编写Mapper类
    • 3.2 编写Reducer类
    • 3.3 编写Driver驱动类
  • 四. 对比及结论


一. Combiner合并的简单介绍

1
  今天我们讲的是Shuffle中的第七步

每一个 map 都可能会产生大量的本地输出,Combiner 的作用就是对map 端的输出先做一次合并,以减少在 map 和 reduce 节点之间的数据传输量,以提高网络IO 性能,是 MapReduce 的一种优化手段之一。

  • 1. Combiner是MR程序中Mapper和Reducer之外的一种组件。
  • 2. Combiner组件的父类就是Reducer。
  • 3. Combiner和Reducer的区别在于运行的位置
    Combiner是在每一个MapTask所在的节点运行
    Reducer是接收全局所有Mapper的输出结果
  • 4. Combiner的意义就是对每一个MapTask的输出进行局部汇总,以减少网络传输量。

二. 通过图片了解使用Combiner和不使用的区别

  • 1. 未使用combiner的网络开销
    2
  • 2. 使用combiner的网络开销
    3

  可以很明显的看出在combiner阶段,通过合并同一个区中相同key的value值,减小了后续的数据传输,从而提高了网络的io!

  但在MapReduce中,combiner是默认不开启的。为什么呢?是因为数据合并并不适用所有的业务需求,如果是计算个数,求和combiner还能发挥它的优势!但如果是求平均数,combiner必不可免的会影响到最终的结果,使结果变得不可靠!所以当我们需要到combiner时,需要手动开启。

  • 3. 自定义Combiner实现步骤
    ①自定义一个Combiner继承Reducer,重写Reduce方法
public class WordcountCombiner extends Reducer<Text, IntWritable, Text,IntWritable>{@Overrideprotected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {// 1 汇总操作int count = 0;for(IntWritable v :values){count += v.get();}// 2 写出context.write(key, new IntWritable(count));}
}

②在Job驱动类中设置:
job.setCombinerClass(WordcountCombiner.class);

三. 代码实现

注:用于对比的程序源代码为《MapReduce系列(2) | 统计输出给定的文本文档每一个单词出现的总次数》中的源代码,有想进行对比的同学,可以自行复制创建对比(其实本源码就比源代码多一行)。

3.1 编写Mapper类

package com.buwenbuhuo.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;/*** @author 卜温不火* @create 2020-04-22 21:24* com.buwenbuhuo.wordcount - the name of the target package where the new class or interface will be created.* mapreduce0422 - the name of the current project.*/
public class WcMapper extends Mapper<LongWritable, Text, Text, IntWritable> {Text k = new Text();IntWritable v = new IntWritable(1);@Overrideprotected 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) {k.set(word);context.write(k, v);}}
}

3.2 编写Reducer类

package com.buwenbuhuo.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;/*** @author 卜温不火* @create 2020-04-22 21:24* com.buwenbuhuo.wordcount - the name of the target package where the new class or interface will be created.* mapreduce0422 - the name of the current project.*/
public class WcReducer extends Reducer<Text, IntWritable, Text, IntWritable>{int sum;IntWritable v = new IntWritable();@Overrideprotected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {// 1 累加求和sum = 0;for (IntWritable count : values) {sum += count.get();}// 2 输出v.set(sum);context.write(key,v);}
}

3.3 编写Driver驱动类

package com.buwenbuhuo.wordcount;
import java.io.IOException;
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;/*** @author 卜温不火* @create 2020-04-22 21:24* com.buwenbuhuo.wordcount - the name of the target package where the new class or interface will be created.* mapreduce0422 - the name of the current project.*/
public class WcDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {// 1 获取配置信息以及封装任务Configuration configuration = new Configuration();Job job = Job.getInstance(configuration);// 2 设置jar加载路径job.setJarByClass(WcDriver.class);// 3 设置map和reduce类job.setMapperClass(WcMapper.class);job.setReducerClass(WcReducer.class);// 4 设置map输出job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);////  仅此一行添加job.setCombinerClass(WcReducer.class);// 5 设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// 6 设置输入和输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));// 7 提交boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

四. 对比及结论

  • 1. 对比
    5

  • 2. Combiner能够应用的前提是不能影响最终的业务逻辑,而且,Combiner的输出kv应该跟Reducer的输入kv类型要对应起来。


本次的分享就到这里了,大家有什么疑惑或者好的建议可以在评论区积极留言。受益的小伙伴们不要忘了点赞关注我呀!!!


http://chatgpt.dhexx.cn/article/1L1F3jov.shtml

相关文章

MapReduce【Shuffle-Combiner】

概述 Conbiner在MapReduce的Shuffle阶段起作用&#xff0c;它负责局部数据的聚合&#xff0c;我们可以看到&#xff0c;对于大数据量&#xff0c;如果没有Combiner&#xff0c;将会在磁盘上写入多个文件等待ReduceTask来拉取&#xff0c;但是如果有Combiner组件&#xff0c;我们…

Mapreduce中Combiner的使用及误区

问题提出&#xff1a; 众所周知&#xff0c;Hadoop框架使用Mapper将数据处理成一个<key,value>键值对&#xff0c;再网络节点间对其进行整理(shuffle)&#xff0c;然后使用Reducer处理数据并进行最终输出。 在上述过程中&#xff0c;我们看到至少两个性能瓶颈&#x…

Hadoop中的MapReduce框架原理、WritableComparable排序案例实操(区内排序)、Combiner合并、自定义 Combiner 实现步骤

文章目录 13.MapReduce框架原理13.3 Shuffle机制13.3.7 WritableComparable排序案例实操&#xff08;区内排序&#xff09;13.3.7.1 需求13.3.7.2 需求分析13.3.7.3 案例实操13.3.7.3.1 增加自定义分区类13.3.7.3.2在驱动类中添加分区类 13.3.8 Combiner合并13.3.8.1 自定义 Co…

Hadoop实例学习(九)Combiner合并

目录 什么是Combiner本质 实例编写Mapper类编写Reducer类编写Driver类结果 什么是Combiner MapReduce中的Combiner就是为了避免map任务和reduce任务之间的数据传输而设置的&#xff0c;Hadoop允许用户针对map task的输出指定一个合并函数。即为了减少传输到Reduce中的数据量。…

MapReduce的combiner

MapReduce的combiner 每一个 map 都可能会产生大量的本地输出&#xff0c; Combiner 的作用就是对 map 端的输出先做一次合并&#xff0c; 以减少在 map 和 reduce 节点之间的数据传输量&#xff0c;以提高网络IO 性能&#xff0c;是 MapReduce 的一种优化手段之一。 combine…

MapReduce中Combiner的作用

问题提出&#xff1a; 众所周知&#xff0c;Hadoop框架使用Mapper将数据处理成一个<key,value>键值对&#xff0c;再网络节点间对其进行整理(shuffle)&#xff0c;然后使用Reducer处理数据并进行最终输出。 在上述过程中&#xff0c;我们看到至少两个性能瓶颈&#x…

Combiner

一、Combiner 1.Combiner是MR程序中Mapper和Reduce之外的一种组件 2.Combiner组件的父类就是Reducer 3.Combiner和Reducer之间的区别在于运行的位置 4.Reducer是每一个接收全局的Map Task 所输出的结果 5.Combiner是在MapTask的节点中运行 6.每一个map都会产生大量的本地输出…

【MapReduce】Combiner详解

Combiner详解 解析Combiner是什么&#xff1f;为什么会出现Combiner&#xff1f;如何使用 CombinerCombiner注意点 代码实现MapperReduceDriver运行日志加上Combiner 解析 Combiner是什么&#xff1f;为什么会出现Combiner&#xff1f; Combiner是一个本地化的reduce操作&…

最简单的js去除首尾空格

function trimStr(str){return str.replace(/(^\s*)|(\s*$)/g,""); } a runoob console.log(trimStr(a));

js去除字符串空格

1、去除字符串内 “所有” 的空格 var str " 1 1 "; var g str.replace(/\s*/g,""); console.log(g) 2、去除字符串内 “两头” 的空格 var str " 1 1 "; var g str.replace(/^\s*|\s*$/g,""); console.log(g);3、去除字符串内…

JS去空格方法

1.trim() 我们知道trim()在IE所支持的版本中&#xff0c;只有IE9以上能支持&#xff0c;所以下面第二个会介绍另一种方法。 2.replace 以下图所示&#xff0c;如果直接这样输入&#xff0c;replace只能去掉一个空格 如果要去掉多个空格用正则表达式&#xff0c;如下图所示&am…

js去掉空格方法-简单一行原生js代码实现

str为要去除空格的字符串: 1、去掉所有空格 strstr.replace(/\s/g,""); //js去掉所有空格 \s表示查找空格带上加好表示连续的空格2、js去掉两头空格 strstr.replace(/^\s|\s$/g,"");//js去掉两头空格3、js去掉左空格 strstr.replace( /^\s*/, ); //…

js字符串去掉前后空格回车换行

问题&#xff1a; 需要规范用户在textarea框中输入的数据&#xff0c;需去掉字符串前后空格回车换行&#xff08;字符串中间的不需要管&#xff09; 解决&#xff1a; 直接使用trim()方法。 var str row.serviceNameModifyList;strstr.trim();//把数据进行去前后的空格和换行案…

【Android -- 蓝牙】蓝牙配对和蓝牙连接

文章目录 一、蓝牙配对二、蓝牙连接 一、蓝牙配对 搜索到蓝牙设备后&#xff0c;将设备信息填充到listview中&#xff0c;点击listiew则请求配对 蓝牙配对有点击配对和自动配对&#xff0c;点击配对就是我们选择设备两个手机弹出配对确认框&#xff0c;点击确认后配对 自动配…

蓝牙协议之配对和绑定学习笔记

蓝牙配对及绑定专业术语 术语描述BDA蓝牙设备地址RPAResolvable Private Address的缩写&#xff0c;可解析的蓝牙设备地址&#xff0c;它会周期性的变化IRK全称&#xff1a;Identity Resolving Key&#xff0c;用于解析蓝牙设备地址的密钥STKShort Term Key&#xff0c;短期密…

android开发之蓝牙配对连接的方法

新年第一篇。 最近在做蓝牙开锁的小项目&#xff0c;手机去连接单片机总是出现问题&#xff0c;和手机的连接也不稳定&#xff0c;看了不少蓝牙方面的文档&#xff0c;做了个关于蓝牙连接的小结。 在做android蓝牙串口连接的时候一般会使用 BluetoothSocket tmp null; // G…

蓝牙配对

蓝牙HC05是主从一体的蓝牙串口模块&#xff0c;简单的说&#xff0c;当蓝牙设备与蓝牙设备配对连接成功后&#xff0c;我们可以忽视蓝牙内部的通信协议&#xff0c;直接将将蓝牙当做串口用。当建立连接&#xff0c;两设备共同使用一通道也就是同一个串口&#xff0c;一个设备发…

Android蓝牙自动配对Demo,亲测好使!!!

蓝牙自动配对&#xff0c;即搜索到其它蓝牙设备之后直接进行配对&#xff0c;不需要弹出配对确认框或者密钥输入框。 转载请注明出处http://blog.csdn.net/qq_25827845/article/details/52400782 源码下载地址&#xff1a;https://github.com/chaohuangtianjie994/BlueTooth-A…

HC05蓝牙模块配对指南(教程)

HC05蓝牙模块配对指南&#xff08;教程&#xff09; 1.准备两个蓝牙模块&#xff0c;一个作为主机&#xff0c;一个作为从机 本人调试过程中用到的是正点原子的HC05蓝牙模块&#xff0c;其余模块的调试大同小异。 2.进入AT状态 进入AT状态有2种方法&#xff1a; 1,上电同时…