在 MapReduce 作业中使用 Avro 时,我看到了一种安静的奇怪行为。事实上,使用的 Iterable 非常奇怪:iterator.next 不指向对象,而是指向在每次调用函数“next”时改变其值的东西!
例子 :
public static class MyAvroReducer extends AvroReducer<Long, MyAvroType,
Pair<Long, MyAvroType>> {
@Override
public void reduce(Long user, Iterable<MyAvroType> listAvroType,
AvroCollector<Pair<Long,MyAvroType>> collector,
Reporter reporter)
throws IOException {
// basically here I am expecting a list of two MyAvroType object
// The first one who has a field "type" equals to "foo" and the second
// who has a filed "type" equals to "bar"
MyAvroType foo;
MyAvroType bar;
for (MyAvroType obj : listAvroType){
if (obj.getType().equals("foo") {foo = obj;}
else if (obj.getType().equals("bar") {bar = obj;}
}
system.out.println("FOO: " + foo.getType());
system.out.println("FOO: " + bar.getType());
}
标准输出说:
FOO:酒吧
酒吧:酒吧
这里的 Iterable 是如何编码的?为什么?或者也许我做错了什么?