我正在尝试使用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?