このためには、単一のレデューサーを使用することをお勧めします。
すべての数値が同じリデューサーに到達するようにするには、次の 2 つのことを行う必要があります。
- マッパーのすべての入力値に対して同じキーを発行する
- reduce タスクをゼロに設定します。
map()
メソッドは次のようになります。
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(new Text("MyAwesomeKey"), key); // assuming that your number is being read in the key
}
Reduce
クラスにはmax
、次のような
プロパティがあります。Long max
メソッドはreduce()
次のようになります。
@Override
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
context.write(new Text("MyAwesomeKey"), key); // assuming that your number is being read in the key
}
次に、オーバーライドするときにrun()
もオーバーライドしreduce()
ます。
public void run(Context context) throws IOException, InterruptedException {
setup(context);
while (context.nextKey()) {
reduce(context.getCurrentKey(), context.getValues(), context);
}
context.write(new LongWritable(max),new Text("")); // write the max value
cleanup(context);
}
reduce タスクを 1 つに設定するには、ジョブのrun()
で次を実行します。これは上記の とは異なることに注意してrun()
ください。
job.setNumReduceTasks(1);
注:上記のコードはすべて新しいmapreduce API に従っています。古いmapred API を使用すると、Reducer の をオーバーライドすることでできるように、Reducer がその仕事を完了した後に単一のフック ポイントを持つことができないと思いますrun()
。