2016-10-10 12 views
2

以下はHadoop Reducerのコードです。なぜ比較が(スラッシュ間に置かれているのか)常に失敗しているのか理解できません。ここでは2つのテキストタイプの値を比較しています。このコードは、反転インデクシングを行うReducer用です。Hadoopのテキスト比較が機能しない

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())); 
    } 
} 

私はこの分野にまったく興味がありません。

答えて

2

Iterable<Text>オブジェクトがTextオブジェクトを再利用している可能性が高いため、同じオブジェクトを再利用するたびに新しいオブジェクトが表示されます。

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

:あなたはこれらの2行を変更する必要が最低で

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

oldValueはいつもあなたが「オブジェクトを指し示すますので、そうでない場合は、あなただけの同じオブジェクトを比較していますそれもまた比較している。

+0

ありがとうございます!それは本当に働いた。私はこの問題を全く知らなかった。 –

関連する問題