2016-07-11 8 views
0

私はhadoopを使ってmapreduceプログラムで作業しています。私は減速中のコードのこの部分を持っている
:私は値を二回繰り返すためにhereを見たよう2回目の反復 - 値は同じままです

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

    long word1count = 0; 
    List<TextLongWritable> cache = new ArrayList<TextLongWritable>(); 

    String decade = key.toString().split("\t")[0]; 
    String word1 = key.toString().split("\t")[1]; 

    for (TextLongWritable val : values) { 
     if (val.getWord().equals("*")){ 
      word1count += val.getCount(); 
      continue; 
     } 
     cache.add(val); 
     log.info("***Reducer*** Word1: " + word1 + " Word2: " + val.getWord()); 
    } 

    context.write(key, new Text("" + word1count)); 

    for (TextLongWritable value : cache) { 
     if (value.getWord().equals("*")){ 
      continue; 
     } 
     log.info("***Reducer*** Word1: " + word1 + " Word2: " + value.getWord()); 
     context.write(new Text(decade + "\t" + value.getWord()), new Text(word1 + " " + value.getCount() + "\t" + word1count)); 
    } 

} 

まず、私はキャッシュを使用しています。

私の問題は、2番目のループでは、すべての値が変わらないことです。たとえば、単語がonetwothreeのリストがあるとします。キーが1900 testなので、word1 = "test"と言います。

最初ロガーの出力は次のようになります。

***Reducer*** Word1: test Word2: one 
***Reducer*** Word1: test Word2: two 
***Reducer*** Word1: test Word2: three 

しかしロガー出力は次のようになります。

***Reducer*** Word1: test Word2: one 
***Reducer*** Word1: test Word2: one 
***Reducer*** Word1: test Word2: one 

値が何らかの理由で同じまま。
ここで何が間違っていますか?それはハープと何か関係がありますか?

答えて

0

thisページを参照して解決することができました。私は実際に最初にこれらすべてのケースを調べましたが、このケースではという2番目の間違った例であるがそのページにあります。

managing iterator in mapreduce投稿で何が起こっているのかについての説明。

私がしなければならなかったことは、それをcacheに追加する前に自分の価値を深くコピーすることでした。完了するために

ここでは私の作業コードです:

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

    long word1count = 0; 
    List<TextLongWritable> cache = new ArrayList<TextLongWritable>(); 

    String decade = key.toString().split("\t")[0]; 
    String word1 = key.toString().split("\t")[1]; 

    for (TextLongWritable val : values) { 
     if (val.getWord().equals("*")){ 
      word1count += val.getCount(); 
      continue; 
     } 
     TextLongWritable val_copy = new TextLongWritable(val.getWord(),val.getCount()); 
     cache.add(val_copy); 
    } 

    context.write(key, new Text("" + word1count)); 

    for (TextLongWritable value : cache) { 
     context.write(new Text(decade + "\t" + value.getWord()), new Text(word1 + " " + value.getCount() + "\t" + word1count)); 
    } 
} 
1

HadoopはGCオーバーヘッドのためにデシリアライズ中に同じオブジェクトをキャッシュします。それをコレクションに入れるためには、TextLongWritableをクローンまたはディープコピーする必要があります。

関連する問題