环境配置
在 Maven 中添加依赖
<dependency>
<groupId>org.apache.mrunit</groupId>
<artifactId>mrunit</artifactId>
<version>1.1.0</version>
<classifier>hadoop2</classifier>
</dependency>
MRUnit测试实例:
MRUnit测试框架基于JUnit。下面是使用MRUnit对MapReduce程序进行单元测试的实例
public class MaxTemperatureMapperTest {
@Test
public void processesValidRecord() throws IOException {
Text value = new Text("0043011990999991950051518004+68750+023550FM-12+0382" +
"99999V0203201N00261220001CN9999999N9-11111+99999999999");
new MapDriver<LongWritable, Text, Text, IntWritable>()
.withMapper(new MaxTemperatureMapper())
.withInput(new LongWritable(0), value)
.withOutput(new Text("1950"), new IntWritable(-1111))
.runTest();
}
}
添加的MaxTemperatureMapper程序
public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private NcdcRecordParser parser = new NcdcRecordParser();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
parser.parse(value);
if(parser.isValid()){
context.write(new Text(parser.getYear()),new IntWritable(parser.getAir()));
}
// String line = value.toString();
// String year = line.substring(15, 19);
// int airTemperature = Integer.parseInt(line.substring(87, 92));
// context.write(new Text(year), new IntWritable(airTemperature));
}
}
根据上面的需求解析类来进行封装
public class NcdcRecordParser {
private static final int MISSING = 9999;
private String year;
private int air;
private String quality;
public void parse(String record){
year = record.substring(15,19);
String airString;
if(record.charAt(87) == '+'){
airString = record.substring(88,92);
}else{
airString = record.substring(87,92);
}
air = Integer.parseInt(airString);
quality = record.substring(92,93);
}
public void parse(Text record){
parse(record.toString());
}
public boolean isValid(){
return air != MISSING && quality.matches("[01459]");
}
public String getYear(){
return year;
}
public int getAir(){
return air;
}
}