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 ...
随机推荐
- IDL实现主成分变化(PCA)
IDL只能通过调用envi的二次接口做图像的变换,但是对于普通的数据没有提供函数.根据主成分变换的原理,用IDL写出来了,这样就不用每次再去用matlab的princomp去做了.主成分变化的基本过程 ...
- ZOJ 3822 Domination(概率dp)
一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...
- spring 读取properties的两种方法
一:直接使用context命名空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi= ...
- 转载:C#中委托、事件与Observer设计模式
原文地址 http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 范例说明 假设 ...
- access的时间相关的查询
string sql = "select * from CONCURRENCY WHERE CONCURRENCY.DATE_FLAG BETWEEN cdate('2013-11-1', ...
- Sql Xtype
SQL Server xtype的介绍 在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行. 列名 数据类型 描 ...
- JS屏蔽右键菜单,复制,粘帖xxxxx........
//屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...
- Hibernate 一对多双向关联Demo
以Classes[班级]和Student[学生]为例的Demo //Classes.java public class Classes implements Serializable { privat ...
- Nginx配置文件nginx.conf详细说明
Nginx配置文件nginx.conf详细说明 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 0000100 ...
- (转载)最实用的清除浮动代码 css的文字过长裁剪后面跟着省略号
css: .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;} .clearfloat ...