MapReduce中常用的 MRunit

环境配置
在 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值