, 나는 AnonymousPair이라는 임의의 WritableComparable 구현이 나는 '복사 생성자를'사용하지 않으면 나는이MapReduce의 감속기 값을 복사해야합니까? 그렇지 않으면 수정해야합니까? 맵리 듀스 응용 프로그램에서
import com.google.common.collect.MinMaxPriorityQueue;
public static class MyReducer extends Reducer<LongWritable, AnonymousPair, LongWritable, Text> {
@Override
protected void reduce(LongWritable key, Iterable<AnonymousPair> values, Context context) throws IOException, InterruptedException {
// ...
MinMaxPriorityQueue<AnonymousPair> pQueue = MinMaxPriorityQueue
.orderedBy(new AnonymousPair().comparator())
.maximumSize(Constants.MaxKeywords)
.create();
for(AnonymousPair val : values) {
pQueue.add(new AnonymousPair(val)); // No problem with copy constructor
// pQueue.add(val); // Wrong! Every element in pQueue will be the same
}
}
}
을 발견했습니다의 pQueue의 모든 요소는 동일한 것으로 끝납니다. 아무도 내가 이것을 이해하도록 도울 수 있습니까? 감사! 내 추측은 다음과 같습니다.
- 감속기 참조 값 요소는 변경 될 수 있습니다. 그것은 문서의 어딘가에 있지만 나는 그것을 놓쳤습니다.
- 내가 잘못
- 또는 내 WritableComparable 구현
내 AnonymousPair 구현
public static class AnonymousPair implements WritableComparable<AnonymousPair> {
private String a = "";
private Float b = 0f;
public AnonymousPair() {}
public AnonymousPair(String a, Float b) {this.a = a; this.b = b;}
public AnonymousPair(AnonymousPair o) {this.a = o.a; this.b = o.b;}
public Comparator<AnonymousPair> comparator() {return new AnonymousPairComparator();}
class AnonymousPairComparator implements Comparator<AnonymousPair> {
@Override
public int compare(AnonymousPair o1, AnonymousPair o2) {
Float diff = o1.b - o2.b;
if(diff == 0) {
return 0;
}
if(diff < 0) {
return 1; // Reverse order
} else {
return -1;
}
}
}
@Override
public int compareTo(AnonymousPair o) {
int temp = this.a.compareTo(o.a);
if(temp == 0) {
return -this.b.compareTo(o.b);
} else {
return temp;
}
}
// More overriding...
}
''AnonymousPair' 클래스와'pQueue'가 구현/정의 된 곳을 표시하십시오. –
'ret' 변수가 선언 된 곳이 표시되지 않습니다. – tsolakp
죄송합니다. 그냥 '돌아와야'합니다. –