下面簡單介紹MRUnit的使用方法
1、下載
http://archive.apache.org/dist/mrunit/mrunit-1.0.0/
本人下載:apache-mrunit-1.0.0-hadoop2-bin.tar.gz
2、安裝
tar -axvf apache-mrunit-1.0.0-hadoop2-bin.tar.gz -C 指定的路徑
將lib下除了commons-logging-1.1.1.jar,其餘的jar都導入到測試工程中
3、測試
本例使用簡化的MaxTemperature實例
Map類:
public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private static final int MISSING = 9999;
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String year = line.substring(0, 4);
int airTemperature;
airTemperature = Integer.parseInt(line.substring(5, 7));
String quality = line.substring(8, 11);
if (airTemperature != MISSING && quality.equals("123"))
context.write(new Text(year), new IntWritable(airTemperature));
}
}
Reduce類:
public class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
for (IntWritable val : values)
maxValue = Math.max(maxValue, val.get());
context.write(key, new IntWritable(maxValue));
}
}
Map測試類:
public class MaxTemperatureMapperTest {
@Test
public void processesValidRecord() throws IOException, InterruptedException {
Text value = new Text("1950-30-123"); //模擬的輸入數據
int result = 30;
new MapDriver<LongWritable, Text, Text, IntWritable>()
.withMapper(new MaxTemperatureMapper()) //map類
.withInput(new LongWritable(0), value) //輸入Text
.withOutput(new Text("1950"), new IntWritable(result)) //預期的輸出結果
.runTest(); //運行測試
}
}
Reduce測試類:
public class MaxTemperatureReducerTest {
@Test
public void returnsMaximumIntegerInValues() throws IOException, InterruptedException {
new ReduceDriver<Text, IntWritable, Text, IntWritable>()
.withReducer(new MaxTemperatureReducer()) //Reduce類
.withInput(new Text("1950"), Arrays.asList(new IntWritable(10), new IntWritable(5))) //模擬的Reduce輸入
.withOutput(new Text("1950"), new IntWritable(10)) //預期的輸出結果
.runTest(); //運行測試
}
}
以jUnit test 方式運行測試類即可