1. 程式人生 > >MapReduce的單元測試框架MRUnit

MapReduce的單元測試框架MRUnit

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
import org.junit.*;

publicclass
AvgTemperatureTest{
MapDriver<LongWritable,Text,Text,IntWritable> mapDriver;
ReduceDriver<Text,IntWritable,Text,IntWritable> reduceDriver;
MapReduceDriver<LongWritable,Text,Text,IntWritable,Text,IntWritable> mapReduceDriver;


@Before
publicvoid setUp(){
AvgTemperatureMapper mapper =newAvgTemperatureMapper
();
AvgTemperatureReducer reducer =newAvgTemperatureReducer();
mapDriver =MapDriver.newMapDriver(mapper);;
reduceDriver =ReduceDriver.newReduceDriver(reducer);
mapReduceDriver =MapReduceDriver.newMapReduceDriver(mapper, reducer);
}

@Test
publicvoid testMapper()throwsIOException{
// Temperature
Text
value =newText("0043011990999991950051518004+68750+023550FM-12+0382"+"99999V0203201N00261220001CN9999999N9-00111+99999999999");

mapDriver.withInput(newLongWritable(), value);
mapDriver.withOutput(newText("1950"),newIntWritable(-11));
mapDriver.runTest();
}

@Test
publicvoid testReducer()throwsIOException{
List<IntWritable> values =newArrayList<IntWritable>();
values.add(newIntWritable(40));
values.add(newIntWritable(42));
reduceDriver.withInput(newText("1950"), values);
reduceDriver.withOutput(newText("1950"),newIntWritable(41));
reduceDriver.runTest();
}

}