CDReducer extends Reducer<LongWritable, CDFitness, LongWritable, CDFitness> { @Override protected void reduce(LongWritable key, Iterable<CDFitness> values, Context context) throws IOException, InterruptedException { int tp = 0; int fp = 0; int tn = 0; int fn = 0; for (CDFitness v : values) { tp += v.getTp(); fp += v.getFp(); tn += v.getTn(); fn += v.getFn(); } context.write(key, new CDFitness(tp, fp, tn, fn)); }    }
@Test public void testReduce() throws Exception { CDReducer reducer = new CDReducer(); Configuration conf = new Configuration(); DummyRecordWriter<LongWritable, CDFitness> reduceWriter = new DummyRecordWriter<LongWritable, CDFitness>(); Reducer<LongWritable, CDFitness, LongWritable, CDFitness>.Context reduceContext = DummyRecordWriter.build(reducer, conf, reduceWriter, LongWritable.class, CDFitness.class); LongWritable zero = new LongWritable(0); reducer.reduce(zero, evaluations, reduceContext); Set<LongWritable> keys = reduceWriter.getKeys(); assertEquals("nb keys", 1, keys.size()); assertTrue("bad key", keys.contains(zero)); assertEquals("nb values", 1, reduceWriter.getValue(zero).size()); CDFitness fitness = reduceWriter.getValue(zero).get(0); assertEquals(expected, fitness); }