1

我正在尝试使用MRUnit 1.0.0 来测试 Hadoop v2 Reducer,但尝试时出现异常:

java.lang.IncompatibleClassChangeError: 
    Found class org.apache.hadoop.mapreduce.TaskInputOutputContext, but interface was expected
                at org.apache.hadoop.mrunit.internal.mapreduce.AbstractMockContextWrapper.createCommon(AbstractMockContextWrapper.java:59)
                at org.apache.hadoop.mrunit.internal.mapreduce.MockReduceContextWrapper.create(MockReduceContextWrapper.java:76)
                at org.apache.hadoop.mrunit.internal.mapreduce.MockReduceContextWrapper.<init>(MockReduceContextWrapper.java:67)
                at org.apache.hadoop.mrunit.mapreduce.ReduceDriver.getContextWrapper(ReduceDriver.java:159)
                at org.apache.hadoop.mrunit.mapreduce.ReduceDriver.run(ReduceDriver.java:142)
                at org.apache.hadoop.mrunit.TestDriver.runTest(TestDriver.java:574)
                at org.apache.hadoop.mrunit.TestDriver.runTest(TestDriver.java:561)

我假设这意味着我在某种程度上不匹配 Hadoop API 的版本,就像在这个 SO question中一样,但我不确定问题出在哪里。我正在使用 Maven 来引入依赖项,使用来自 repo.hortonworks.com 的 Hadoop 2.2.0.2.0.6.0-76 和来自 repo1.maven.org 的 MRUnit 1.0.0:

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-mapreduce-client-core</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-mapreduce-client-common</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-yarn-common</artifactId>
    <version>2.2.0.2.0.6.0-76</version>
</dependency>
<dependency>
    <groupId>org.apache.mrunit</groupId>
    <artifactId>mrunit</artifactId>
    <version>1.0.0</version>
    <classifier>hadoop2</classifier>
</dependency>

测试用例如下:

@Test
public void testReducer() throws IOException, InterruptedException {
    HH.Reduce r = new HH.Reduce();

    T1 fx1 = new T1();
    T1 fx2 = new T1();

    List<T1> values = new ArrayList<T1>();
    values.add(fx1);
    values.add(fx2);

    T1 fxBoth = new T1(fx1.size() + fx2.size());
    fxBoth.addValues(fx1);
    fxBoth.addValues(fx2);


    ReduceDriver<NullWritable, T1, NullWritable, T1> reduceDriver = ReduceDriver.newReduceDriver(r);

    reduceDriver.withInput(NullWritable.get(), values);
    reduceDriver.withOutput(NullWritable.get(), fxBoth);

    // TODO I can't seem to get this test to work.  
    // Not sure what I'm doing wrong, whether it's a real 
    // problem or a testing problem.
    reduceDriver.runTest();
}

在其他地方,在HH包中,Reduce 被定义为一个非常简单的内部类:

public static class Reduce extends Reducer<NullWritable, T1, NullWritable, T1> {
    @Override
    public void reduce(NullWritable key, Iterable<T1> values, Context context)
        throws InterruptedException, IOException {

        // Need to create a new record here, because the one we're handed
        // may be recycled by our overlords.
        T1 out = new T1();
        for (T1  t : values) {
            out.addValues(t);
        }
        context.write(key, out);
    }
}

看到什么奇怪的东西了吗?MRUnit 是否尝试使用旧/新版本的 API?

4

2 回答 2

0

我相信我有同样的问题,但我使用 hadoop-core.1.2.1 和 mrunit-hadoop2-1.1.0 。检查 maven 依赖项中的版本和分类器(用于测试,而不是 pom.xml 中声明的那些)。

于 2014-07-31T20:03:35.413 回答
0

mrunit maven 依赖中的分类器部分非常重要。

正如您所说,您使用的是 hadoop-core.1.2.1,TaskAttemptContext 是该 jar 中的一个类。所以你需要在mrunit的maven依赖中将分类器设置为hadoop1。然后这没有任何问题。

如果您将分类器设置为 hadoop2,则它需要最新的 api,其中 TaskAttemptContext 是接口。您可以简单地在 junit 中运行该文件并检查结果。

于 2014-11-27T13:32:58.547 回答