最近写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用的是同一个引用的更多相关文章

  1. 使用ES6的reduce函数,根据key去重

    最近很着迷于ES6的函数,让代码变得更优雅.ES6里的reduce函数,平时用的不是特别多,真正用起来发现还是挺好用的. 想要实现的效果为: 原数组: let rawArr = [{id:'123'} ...

  2. python里使用reduce()函数

    reduce()函数在库functools里,如果要使用它,要从这个库里导入.reduce函数与map函数有不一样地方,map操作是并行操作,reduce函数是把多个参数合并的操作,也就是从多个条件简 ...

  3. Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里

    reduce函数:在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引入:>>> from functool ...

  4. Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数

    Python第七天   函数  函数参数   函数里的变量   函数返回值  多类型传值     函数递归调用   匿名函数   内置函数 目录 Pycharm使用技巧(转载) Python第一天   ...

  5. python3中reduce()函数的使用方法示例

      reduce() 函数会对参数序列中元素进行累积,下面这篇文章主要给大家介绍了关于python中reduce()函数的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学 ...

  6. MapReduce之Reduce Join

    一 介绍 Reduce Join其主要思想如下: 在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag), 比如:t ...

  7. Hadoop提供的reduce函数中Iterable 接口只能遍历一次的问题

    今天在写MapReduce中的reduce函数时,碰到个问题,特此记录一下: void reduce(key, Iterable<*>values,...) { for(* v:value ...

  8. python Map()和reduce()函数

    Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...

  9. Entity Framework 6 Recipes 2nd Edition(11-4)译 -> 在”模型定义”函数里调用另一个”模型定义”函数

    11-4.在”模型定义”函数里调用另一个”模型定义”函数 问题 想要用一个”模型定义”函数去实现另一个”模型定义”函数 解决方案 假设我们已有一个公司合伙人关系连同它们的结构模型,如Figure 11 ...

随机推荐

  1. Android运行时注解

    Android的注解有编译时注解和运行时注解,本文就介绍下运行时注解. 其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用fin ...

  2. sql server 2008 评估期已过期

    解决办法: 修改注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\ConfigurationState里的&quo ...

  3. jquery的ajax方法:ajaxStart()和ajaxStop()

    ajaxStart()方法: 当AJAX请求开始时,显示加载中的提示. $("#divMessage").ajaxStart(function(){ $(this).show(); ...

  4. 面试题——分析从输入url到页面返回的过程(或者查询返回过程)

    1. You enter a URL into the browser(输入一个url地址) 2.The browser looks up the IP address for the domain ...

  5. 用Global.asax实现伪静态.

    在Global.asax文件里添加Application_BeginRequest事件处理.添加如下代码: 1 protected void Application_BeginRequest(Obje ...

  6. drop table xx purge

    drop table xx purge; 说明: 所有删除的表都会在回收站里面,只有后面加上purge才是彻底的清空表. (一般用于测试.练习数据表,所以最好不要带purge,要不误删就找不到了.)

  7. MySQL 删除数据表

    MySQL 删除数据表 MySQL中删除数据表是非常容易操作的, 但是你再进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失. 语法 以下为删除MySQL数据表的通用语法: DROP TA ...

  8. linux术语解析(持续更新)

    1.linux内核有个版本,分别是 longterm: 提供长期支持的内核版本 stable: 稳定版本 Beta 测试版

  9. C++变量的“总分性”(Mereology)

    Stroustrup 在自传中说自己在哲学上深受 Kierkegaard (吉爾凱高爾)的影响,而讨厌黑格尔.所以看 Stroustrup 的书,很少感受到抽象理论的重要性.这也影响了C++的文化:许 ...

  10. 用core dump来调试程序段错误

    有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来 ...