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 ...
随机推荐
- C++ XML 解释库
rapidxml http://rapidxml.sourceforge.net/index.htm
- CALayer的基本操作
CALayer简介: CALayer又称为层. 在每一个UIView内部都有一个Layer这样的属性. UIView之所以能够显示,就是因为它里面有这个一个层,才具有显示的功能. 我们通过操作C ...
- 对于android拦截短信的一些疑问
最近折腾android4.4短信拦截的问题,要求在app上收到短信的时候弹出提示,并显示的功能. 然后找到了使用broadcastreceiver和contentprovider两种方法,那么问题来了 ...
- Java基础知识强化之IO流笔记13:递归之不死神兔问题(斐波那契数列)
1.这个问题是如下的: 有一对兔子,从出生后第3个月起,每个月都生一对兔子,小兔子长到第3个月又生一对兔子,加入兔子都不死,问第20个月兔子的对数? 分析:我们找规律 兔子对数第1个月: 1 ...
- SimpleDateFormat使用简析
title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...
- codevs 1213 解的个数(我去年打了个表 - -)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int T,x ...
- 关于typedef int(*lpAddFun)(int, int)
lpAddFun是typedef定义的一个名称 可以用来定义变量 比如 lpAddFun p; 那 p就是 int(*p)(int, int); 首先(*p)说明p是一个指针,(*p)();说明p指向 ...
- static_cast、const_cast和reinterpret_cast学习
static_cast 任何具有明确定义的类型转换,只要不包含底层const,都可以使用static_cast.例如,通过将一个运算对象强制转换成double类型就能表达式浮点数除法: //进行强制类 ...
- 外边距叠加collapsing-margin
原载:Smallni | http://www.smallni.com/collapsing-margin/ 恩,margin叠加一直是个问题,也是我们一直会遇到的问题,很久以前就想把这个知识点整理下 ...
- php while循环 指定显示内容 例如不想显示前10条和后10条
<?php //查询信息总的条数 $db_num = query_num("表","where 1=1"); //每页显示的条数 $page_size=2 ...