Wordcount -- MapReduce example -- Reducer
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的更多相关文章
- Wordcount -- MapReduce example -- Mapper
Mapper maps input key/value pairs into intermediate key/value pairs. E.g. Input: (docID, doc) Output ...
- Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类
前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...
- MapReduce原理与设计思想
简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家 ...
- hadoop2.2.0的WordCount程序
package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ...
- MapReduce极简教程
一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家数自己手中的牌有几张是黑桃,然后 ...
- 大数据 --> MapReduce原理与设计思想
MapReduce原理与设计思想 简单解释 MapReduce 算法 一个有趣的例子:你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座 ...
- 如何在Windows下面运行hadoop的MapReduce程序
在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...
- 【Hadoop】Hadoop mr wordcount基础
1.基本概念 2.Mapper package com.ares.hadoop.mr.wordcount; import java.io.IOException; import java.util.S ...
- 转:MapReduce原理与设计思想
转自:http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html 简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张 ...
随机推荐
- java判断类型
判断是否String:str.getClass().getName().equals("java.lang.String") 判断是否在且不为空:Object.hasKey(&qu ...
- SSH整合(一)
一.ssh原始整合方式 不需要任何整合包,就是简单的将三个框架集合到一起 hibernate 导入jar包: hibernate-release-5.0.7.Fin ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 048-49
今日刷题048-049 ------------------------------------------------L1-048---------------------------------- ...
- Windows10:Opencv4.0+Opencv4.0.1_contrib编译
操作系统:windows10 64bit 已安装工具:VS2017 64bit,cmake3.12bit. 安装Cmake:到cmake下载3.12及以上版本,64bit, 选择windows下的安装 ...
- Struts2+Spring+Hibernate整合开发(Maven多模块搭建)
Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...
- redis集群部署步骤
1.yum 安装依赖 yum install gcc unzip wget 2.编译安装redis,编译安装的目的是源码包内包含了接下来创建redis集群所需要的 redis-trib.rb脚本 ma ...
- PHP Mysql字符集utf8mb4支持Emoji表情
项目开发中经常会遇到用户在评论或者发表文章的时候会打一些表情在里面,如果我们在开发中不去做一些处理的话,表情会出不来的,甚至是报错,下面简单介绍处理方式.原文地址:小时刻个人博客:http://sma ...
- 单片机-C语言-定义和申明
以下代码是单片机程序,51单片机,编译器为HT-IDE3000, 简单来说 头文件中只能申明, 变量在头文件中申明时,要加上extern 这个关键字用来告诉编译器,变量在其它的文件中定义,为什么要在头 ...
- Quartus II 项目文件分类及内容
- ChipScope Pro Inserter - "ERROR:NgdBuild:924 - bidirect pad net '<oDRAM0_A>' is driving non-buffer primitives
解决方案: Using a IOBUF signal as a trigger for the ILA Inserter flow will cause a NGDBuild error. These ...