MapReduce的reduce函数里的key用的是同一个引用
最近写MapReduce程序,出现了这么一个问题,程序代码如下:
package demo; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class ReducerDemo extends Reducer<Text, IntWritable, Text, IntWritable>{ private FileSystem fs = null;
private FSDataOutputStream outs = null;
public Map<Text, Integer> wordNumMap = new HashMap<Text, Integer>(); @Override
protected void setup(Context context)
throws IOException, InterruptedException {
String logFile = context.getConfiguration().get(HdpDemo.LOG_FILE);
fs = FileSystem.get(context.getConfiguration());
if(null != logFile){
int taskId = context.getTaskAttemptID().getTaskID().getId();
logFile += ("_"+taskId);
outs = fs.create(new Path(logFile));
}
} /* public void reduce(Text key, IntWritable value, Context context){ }*/ public void reduce(Text key, Iterable<IntWritable> numberIter, Context context)
throws IOException, InterruptedException {
Text word = key;
Integer currNum = wordNumMap.get(word);
if(null == currNum){
currNum = 0;
}
for(IntWritable num:numberIter){
currNum += num.get();
}
wordNumMap.put(word, currNum); } @Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
for(Entry<Text, Integer> entry : wordNumMap.entrySet()){
IntWritable num = new IntWritable(entry.getValue());
context.write(entry.getKey(), num);
}
outs.close();
} private void log(String content) throws IOException{
if(null != outs){
outs.write(content.getBytes());
}
} }
这是个单词统计的reducer类,按理说打印出来的结果应该是如下结果:
world
ccc
of
best
the
is
bbb
james
ddd
hello
aaa
而实际上的打印结果却为:
world:
world:
world:
world:
world:
world:
world:
world:
world:
world:
world:
原因分析如下:
Hadoop的MapReduce框架每次调用reducer的reduce函数,代码中的第39行,每次传入的key都是对同一个地址的引用,导致了插入wordNumMap中的那些key都被修改了。
而如果把第41行的
Text word = key;
改为
Text word = new Text();
word.set(key);
这样结果就正确了,也印证了我的猜测。
MapReduce的reduce函数里的key用的是同一个引用的更多相关文章
- 使用ES6的reduce函数,根据key去重
最近很着迷于ES6的函数,让代码变得更优雅.ES6里的reduce函数,平时用的不是特别多,真正用起来发现还是挺好用的. 想要实现的效果为: 原数组: let rawArr = [{id:'123'} ...
- python里使用reduce()函数
reduce()函数在库functools里,如果要使用它,要从这个库里导入.reduce函数与map函数有不一样地方,map操作是并行操作,reduce函数是把多个参数合并的操作,也就是从多个条件简 ...
- Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里
reduce函数:在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引入:>>> from functool ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- python3中reduce()函数的使用方法示例
reduce() 函数会对参数序列中元素进行累积,下面这篇文章主要给大家介绍了关于python中reduce()函数的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学 ...
- MapReduce之Reduce Join
一 介绍 Reduce Join其主要思想如下: 在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag), 比如:t ...
- Hadoop提供的reduce函数中Iterable 接口只能遍历一次的问题
今天在写MapReduce中的reduce函数时,碰到个问题,特此记录一下: void reduce(key, Iterable<*>values,...) { for(* v:value ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- Entity Framework 6 Recipes 2nd Edition(11-4)译 -> 在”模型定义”函数里调用另一个”模型定义”函数
11-4.在”模型定义”函数里调用另一个”模型定义”函数 问题 想要用一个”模型定义”函数去实现另一个”模型定义”函数 解决方案 假设我们已有一个公司合伙人关系连同它们的结构模型,如Figure 11 ...
随机推荐
- Android短信的发送和接收监听
/**发送与接收的广播**/ String SENT_SMS_ACTION = "SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION = & ...
- Java基础知识强化之IO流笔记12:递归之递归解决问题的思想(图解)
1. 使用递归计算5!的结果,递归思想的本质如下: 2. 下面就要使用代码实现这个递归: 递归实现分析: (1)做递归要写一个方法 (2)出口条件 (3)规律 代码实现如下: package com. ...
- ubuntu 查看端口被占用并处理
当启动程序出现端口号被占用的情况,需要查看端口使用情况,使用netstat命令,下面是常用的几个查看端口情况的命令:查看所有的服务端口(ESTABLISHED netstat -a查看所有的服务端口, ...
- Struts2里如何取得request,session,application
第一种:取得MAP类型的request,session,application在java文件里写 package com.xjtu.st; import java.util.Map; import c ...
- HTML5 History对象,Javascript修改地址栏而不刷新页面
一.History对象 History 对象包含用户(在浏览器窗口中)访问过的 URL. History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. ...
- (Error) The type AESKeyGenerator is not accessible due to restriction on required library.
error for 'Access restriction: The type AESKeyGenerator is not accessible due to restriction on requ ...
- oracle特殊字符的ascii值
- 用XMPP实现完整Android聊天项目
简介 这是一个完整的xmpp的Android的项目服务端使用openfire3.9.3客户端使用Android4.2.2 集成第三方:百度地图sdkasmack.jaruniversal-image- ...
- MySQL 插入数据
MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...
- Ajax的工作流程简述
提到Ajax相信我们都不会陌生,不管你是前端开发还是后台数据处理的程序员,ajax的作用就像现在生活中的手机一样,无论是作用还是流程都差不多,这里我们要进行ajax操作后台数据并显示在页面上的话,首先 ...