【问题标题】:Hadoop Text Comparison not workingHadoop文本比较不起作用
【发布时间】:2017-02-19 06:43:47
【问题描述】:

下面是 Hadoop Reducer 的代码,我不明白为什么比较(放在斜杠之间)总是失败,这里我们比较两个 Text 类型的值。此代码用于执行反向索引的 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()));
    }   
}

感谢任何帮助,因为我对这个领域完全陌生。

【问题讨论】:

    标签: java hadoop mapreduce reducers


    【解决方案1】:

    您的问题很可能与 Iterable&lt;Text&gt; 正在重用 Text 对象这一事实有关,因此它不会每次都给您一个新对象,它只是重用同一个对象。

    您至少需要更改这两行:

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

    收件人:

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

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

    【讨论】:

    • 谢谢!它真的奏效了。我完全不知道这个问题。
    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多