MRUnit测试用于测试hadoop map程序错误

时间:2017-09-27 03:49:09

标签: java hadoop mapreduce mrunit

我试图使用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 的子类 所以我不确定这里有什么问题。

以下是完整的Mapper和Unit测试类

MaxTempMapper

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();
        }
    }

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

以下是Intellij的截图

Screenshot

1 个答案:

答案 0 :(得分:0)

您需要更改:

class JWTRefreshMiddleware extends RefreshToken { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException * * @return mixed */ public function handle($request, Closure $next) { $response = parent::handle($this->addUserToRequest($request), $next); return $response; } private function addUserToRequest(\Illuminate\Http\Request $request): \Illuminate\Http\Request { $user = User::find(JWTAuth::manager()->decode(JWTAuth::getToken())['user_id']); $request->merge(['user' => $user]); $request->setUserResolver(function () use ($user) { return $user; }); Auth::setUser($user); return $request; } }

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

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