我在学MapReducer时遇到了因为key,value对象重用导致输出异常的情况,当时为验证key,val的重用在reduce中加输出语句输出对象的Hash Code,将不同key放入一个reduce中去执行,结果发现只执行了一次,在查询源码后发现
public void run(Context context) throws IOException, InterruptedException {
setup(context);
try {
while (context.nextKey()) {
reduce(context.getCurrentKey(), context.getValues(), context);
// If a back up store is used, reset it
Iterator<VALUEIN> iter = context.getValues().iterator();
if(iter instanceof ReduceContext.ValueIterator) {
((ReduceContext.ValueIterator<VALUEIN>)iter).resetBackupStore();
}
}
} finally {
cleanup(context);
}
然后我发现是有循环的过程的,在方法中输出key值和context.nextkey()返回为第一个key值和FALSE,也就是说没有了循环的过程,但是结果却是循环完成的。而且context在方法中是一个抽象内部类实现reduceContext接口,context.nextKey()在reduce()方法中是一个抽象方法,并没有给出实现方法,它在哪里实现的?循环的过程又是在哪里完成的?