Hadoop 文本比较不起作用

Hadoop Text Comparison not working

下面是Hadoop Reducer的代码,我不明白为什么比较(放在斜杠之间)总是失败,这里我们比较两个文本类型的值。此代码用于 Reducer 执行倒排索引。

 public static class IntSumReducer
       extends Reducer<TextPair, Text, Text, Text>{

    private Text indexedData = new Text();

    public void reduce(TextPair key, Iterable<Text> values, Context context)
           throws IOException, InterruptedException {

        Iterator<Text>  itr = values.iterator();
        Text oldValue = itr.next() ;
        String old = oldValue.toString();

        //String next;
        int freq = 1;
        Text nextValue = null;
        StringBuilder stringBuilder = new StringBuilder();

        if(itr.hasNext()==false) {
            stringBuilder.append(old + 1);
        }

        while(itr.hasNext()) {
            nextValue = itr.next();         
            int compareValue = oldValue.compareTo(nextValue);

            while(compareValue == 0) {
                freq++;

                if(itr.hasNext()) {
                    nextValue = itr.next();

                   ////////////////////////////
                   // following comparison always returning zero
                   // Although values are changing
                   compareValue = oldValue.compareTo(nextValue);
                   ///////////////////////////

                    System.out.println(compareValue);

                } else {
                    freq++;
                    System.out.println("Break due to data loss..");
                    break;
                }               
            }//end while
            System.out.println("Value Changed..");
            old = old + freq;
            stringBuilder.append(old);
            stringBuilder.append(" | ");
            oldValue = nextValue;
            old = nextValue.toString();
            freq = 1;

        }//endwhile

        //System.out.println("KEY :: " + key.toString());   
        context.write(key.getFirst(),new Text(stringBuilder.toString()));
    }   
}

感谢任何帮助,因为我是这个领域的新手。

你的问题很可能与 Iterable<Text> 重用 Text 对象有关,所以它不会每次都给你一个新对象,它只是重用同一个对象。

至少你需要改变这两行:

Text oldValue = itr.next();
oldValue = nextValue;

收件人:

Text oldValue = new Text(itr.next());
oldValue.set(nextValue);

否则你只是在比较同一个对象,因为 oldValue 将始终指向你正在比较的对象。