Database Reference
In-Depth Information
@Test
public void returnsMaximumIntegerInValues () throws IOException ,
InterruptedException {
new ReduceDriver < Text , IntWritable , Text , IntWritable >()
. withReducer ( new MaxTemperatureReducer ())
. withInput ( new Text ( "1950" ),
Arrays . asList ( new IntWritable ( 10 ), new IntWritable ( 5 )))
. withOutput ( new Text ( "1950" ), new IntWritable ( 10 ))
. runTest ();
}
We construct a list of some IntWritable values and then verify that MaxTemperat-
ureReducer picks the largest. The code in Example 6-9 is for an implementation of
MaxTemperatureReducer that passes the test.
Example 6-9. Reducer for the maximum temperature example
public class MaxTemperatureReducer
extends Reducer < Text , IntWritable , Text , IntWritable > {
@Override
public void reduce ( Text key , Iterable < IntWritable > values , Context
context )
throws IOException , InterruptedException {
int maxValue = Integer . MIN_VALUE ;
for ( IntWritable value : values ) {
maxValue = Math . max ( maxValue , value . get ());
}
context . write ( key , new IntWritable ( maxValue ));
}
}
Search WWH ::




Custom Search