Reducer receives (key, values) pairs and aggregate values to a desired format, then write produced (key, value) pairs back into HDFS.

E.g.

Input: (term, [1, 1, 1, 1])

Output: (term, 4)

Reducer Class Prototype:

Reducer<Text, IntWritable, Text, IntWritable>
// Text:: INPUT_KEY
// IntWritable:: INPUT_VALUE
// Text:: OUTPUT_KEY
// IntWritable:: OUTPUT_VALUE

Reduce Method for Mapper

Method header

public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException
// Text key:: Declare data type of input key;
// Iterable<IntWritable> values:: Declare data type of input values; (Note: Received values from mapper should be in a list)
// Context context:: Declare data type of output. Context is often used for output data collection.

Aggregate Values

// Iterate through all the values wrt the key:
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}

Building (key, value) pairs

// Convert built-in int into IntWritable
result.set(sum);
// build (key, value) pair into Context and emit:
context.write(key, result);

Reducer Class Summary

Reducer class produces Reducer.Context object and serialize obtained (key, value) pair into HDFS.

Overview of Reducer Class

public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}

Written with StackEdit.

Wordcount -- MapReduce example -- Reducer的更多相关文章

  1. Wordcount -- MapReduce example -- Mapper

    Mapper maps input key/value pairs into intermediate key/value pairs. E.g. Input: (docID, doc) Output ...

  2. Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类

    前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...

  3. MapReduce原理与设计思想

    简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家 ...

  4. hadoop2.2.0的WordCount程序

    package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ...

  5. MapReduce极简教程

    一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃?   MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家数自己手中的牌有几张是黑桃,然后 ...

  6. 大数据 --> MapReduce原理与设计思想

    MapReduce原理与设计思想 简单解释 MapReduce 算法 一个有趣的例子:你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座 ...

  7. 如何在Windows下面运行hadoop的MapReduce程序

    在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...

  8. 【Hadoop】Hadoop mr wordcount基础

    1.基本概念 2.Mapper package com.ares.hadoop.mr.wordcount; import java.io.IOException; import java.util.S ...

  9. 转:MapReduce原理与设计思想

    转自:http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html 简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张 ...

随机推荐

  1. CSS之元素

    CSSS书写位置 内嵌式 <head> <style type = "text/css"> **** </style> </head> ...

  2. React--- react 初见React 总结

    简介 react 程序代码是透明的,需要什么装什么 代码实现逻辑清晰可见 第一天 React  基础构造 分别是  继承的 React.component(继承的依赖类)/dom(dom元素)/pro ...

  3. 第一课、安装登录CentOS7

    一.学习之初 1.学习这个课程的目的是,高薪就业,搞运维. 2.应该在宁波发展. 3.大概给自己定的计划是4个月能学习2遍. 4.学好之后就跳槽. 5.2年左右的时间要达到1.5W争取. 学习方法: ...

  4. go加密算法:CBC对称加密(一)--3DES/AES

    其实对称加密中的:DES\3DES\AES 采取的加解密步骤一致,只是小的细节不太一样.大家多看看就能写出来了 // rsao1.go package main import ( "byte ...

  5. ThinkPHP5.1完全开发手册.CHM离线版下载

    ThinkPHP5.1完全开发手册.CHM离线版下载 ThinkPHP5.1完全开发手册离线版.CHM下载地址 百度云:链接: https://pan.baidu.com/s/1b4jKJN-8UyI ...

  6. Order Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messag ...

  7. django使用pycharm为项目选择虚拟环境-3.1

    使用pycharm打开项目 选择右上角的 file - settings - project - project interpreter 选择左上的设置符号,选择 Add 选择红框部分,然后选择之前创 ...

  8. Python习题(分页显示)

    class Page: def __init__(self, lst, pageSize): self.lst = lst # 数据 self.pageSize = pageSize # 每页显示多少 ...

  9. Java学习笔记十七:Java中static使用方法

    Java中static使用方法 一:Java中的static使用之静态变量: 我们都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的 ...

  10. burp实时获取token

    在一些web网站里 会加入token来限制用户的一些操作 如果用户的请求里面没有这个token  那么我们的一些操作就会很麻烦 现在 我来演示一下burp如何自动更新token 首先 需要dvwa  ...