2

我正在尝试使用 MRUnit 对 Mapper 程序进行单元测试(来自 Hadoop:权威指南,第 153 页,部分:使用 MRUnit 编写单元测试:Mapper)。我正在使用 intellij Idea,它显示方法错误

new org.apache.hadoop.mrunit.MapDriver<>().withMapper(myMapper)

错误消息说, MapDriver 中的 withMapper(org.apache.hadoop.mapreduce.Mapper) 不能应用于 (complexmapreduce.MaxTempMapper)

MaxTempMapper被声明为org.apache.hadoop.mapreduce.Mapper的子类 ,所以我不太确定这里出了什么问题。

这是完整的映射器和单元测试类

最大温度映射器

package complexmapreduce;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class MaxTempMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private static final int MISSING = 9999;
    private NDCRecordParser myParser = new NDCRecordParser();

    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        myParser.parse(value);
        if (myParser.isValidTemperature()) {
            context.write(new Text(myParser.getYear()), new IntWritable(myParser.getMaxTemperature()));
        }
    }
}

MaxTempUnitTest

    package complexmapreduce;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.junit.Test;
    import java.io.IOException;

    public class MaxTempSingleLineUnitTest {

        @Test
        public void testMaxTempMapper() throws IOException {
            Text value = new Text("0029029070999991901010106004+64333+023450FM-12+000599999V0202701N015919999999N0000001N9-00781+99999102001ADDGF108991999999999999999999");
            LongWritable key = new LongWritable(0);
            MaxTempMapper myMapper = new MaxTempMapper();
            new org.apache.hadoop.mrunit.mapreduce.MapDriver<>()
                    .withMapper(myMapper)     // <<<===Error here
                    .withInput(key, value)
                    .withOutput(new Text("1901"), 
                     new IntWritable(0210))
                    .runTest();
        }
    }

注意:已经尝试过这里的解决方案,但没有运气。

这是 Intellij 的屏幕截图

截屏

4

1 回答 1

0

你需要改变:

new org.apache.hadoop.mrunit.mapreduce.MapDriver<>()

new org.apache.hadoop.mrunit.mapreduce.MapDriver<LongWritable, Text, Text, IntWritable>()

您需要添加泛型类型,以便它知道如何运行映射器。

于 2017-09-27T08:51:57.777 回答