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 算法 一个有趣的例子 你想数出一摞牌中有多少张 ...
随机推荐
- Oracle 体系结构二 内存结构
Oracle实例由共享内存块(SGA)以及大量的后台进程构成. SGA必须包含的数据结构: 数据库缓冲区缓存 日志缓冲区 共享池 可选的数据结构: 大池 JAVA池 流池 其他缓冲区缓存池 用户会话还 ...
- Web—02-轻松理解css
CSS基本语法以及页面引用 CSS基本语法 css的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个 ...
- Swift_属性
Swift_属性 点击查看源码 class DataImporter { var fileName = "data.txt" init() { print("初始化&qu ...
- iOS之改变UIAlertViewController字体的颜色
NSString *message = @"请确认信息是否正确?"; NSString *title = @"提示"; UIAlertController *a ...
- 浅谈React、Vue 部分异步
React中的setState setState为什么需要异步? 无法限制何时使用异步,多次连续使用setState 防止多次渲染,异步rendering不仅仅是性能上的优化,而且这可能是react组 ...
- JS中的“==”与强制类型转换
JavaScript中有“==”与“===”,那么他们有何区别呢? 对于基本数据类型, === (!==)只有当两个变量的类型和值都相等时,才返回true:而 == (!=)则会对变量进行强制类型转 ...
- vector 定义的二维数组的遍历
之前我们分享了STL的一些容器,再介绍vector中只介绍了二维的vector的定义并没有说二维的vector怎么遍历,那么我们今天就来看下二维的vector怎么遍历 看下面的代码吧. #includ ...
- ThinkPHP5.1完全开发手册.CHM离线版下载
ThinkPHP5.1完全开发手册.CHM离线版下载 ThinkPHP5.1完全开发手册离线版.CHM下载地址 百度云:链接: https://pan.baidu.com/s/1b4jKJN-8UyI ...
- 使用NPOI将数据导出Excel
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.IS ...
- Oracle之多表查询
-多表查询 1.交叉连接 select * from t_class for update; select * from t_student for update; select for update ...