/ 在hadoop reduce方法中,我将TopFrequency对象添加到PriorityQueue中。 假设我的reducer类中有getTotalCount方法。 totalCount是DoubleWritable类型的实例变量。 /
class TopFrequency{
private double relativeFrequency;
private WordPair wordPair;
@Override
public int compareTo(TopFrequency arg0){
int i=this.wordPair.compareTo(arg0.wordPair);
if(this.relativeCount<arg0.relativeCount){
return -1;
}else if(this.relativeCount>arg0.relativeCount){
return 1;
}else if(this.relativeCount==arg0.relativeCount){
return 0;
}
return i;
}
}
class WordPair{
private Text word;
private Text neighbor;
}
class Reduce extends Reducer{
@覆盖
protected void reduce(WordPair键,可迭代值,上下文上下文){
double rCount = 0.0;
int count = getTotalCount(values);
relativeFrequency=((double)count/totalCount.get());
relativeCount.set((double) count / totalCount.get());
//Creating an object of TopFrequency
TopFrequency topFrequency=new TopFrequency(key, relativeFrequency);
// All the references in reducerQueue point to recently added object
reducerQueue.add(topFrequency);
System.out.println("After adding to the queue "+reducerQueue);
}
}