【问题标题】:MRUnit Example for MultipleOutputs多输出的 MRUnit 示例
【发布时间】:2023-03-30 23:20:01
【问题描述】:

我写了一个 Map only hadoop 作业,我在其中使用了 MultipleOutputs 概念。这里的问题是,我想用 MRUnit 测试这段代码。我没有看到 MultipleOutputs 测试的任何工作示例。

我的映射器代码会是这样的,

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

    String inputString = value.toString();
    String outputString = null;
    Text resultValue = null;

    String finalResult = null;
    String exceptionMessage = null;

    try {

        outputString = processInput(dataSet, inputString);

    } catch (MalformedURLException me) {
        System.out.println("MalformedURLException Occurred in Mapper:"
                + me.getMessage());
        exceptionMessage = me.getMessage();
    } catch (SolrServerException se) {
        System.out.println("SolrServerException Occurred in Mapper:"
                + se.getMessage());
        exceptionMessage = se.getMessage();
    } 
    if (outputString == null || outputString.isEmpty()
            && exceptionMessage != null) {
        exceptionMessage = exceptionMessage.replaceAll("\n", ", ");
        finalResult = inputString + "\t[Error] =" + exceptionMessage;
        resultValue = new Text(finalResult);
        multipleOutputs.write(SearchConstants.FAILURE_FILE,NullWritable.get(), resultValue);
    } else {
        finalResult = inputString + outputString;
        resultValue = new Text(finalResult);
        multipleOutputs.write(SearchConstants.SUCCESS_FILE,NullWritable.get(), resultValue);
    }

}

你们谁能给我一个使用 MultipleOutputs 进行 MRUnit 测试的工作示例吗?

【问题讨论】:

    标签: mapreduce elastic-map-reduce amazon-emr mrunit multipleoutputs


    【解决方案1】:

    这是一个稍微简化的类的示例

    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.NullWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
    
    import java.io.IOException;
    
    public class SomeMapper extends Mapper<LongWritable, Text, NullWritable, Text> {
        public static final String SUCCESS_FILE = "successFile";
        private static MultipleOutputs<NullWritable, Text> multipleOutputs;
        private static Text result = new Text();
    
        @Override
        public void setup(Context context) throws IOException, InterruptedException {
            multipleOutputs = new MultipleOutputs<>(context);
            super.setup(context);
        }
    
        @Override
        public void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException {
            String outputString = "some result";  // logic here
    
            result.set(outputString);
            multipleOutputs.write(SUCCESS_FILE, NullWritable.get(), result);
        }
    }
    
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.NullWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
    import org.apache.hadoop.mrunit.mapreduce.MapDriver;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({MultipleOutputs.class, SomeMapper.class})
    public class SomeMapperTest {
        @Test
        public void someTest() throws Exception {
            MapDriver<LongWritable, Text, NullWritable, Text> mapDriver = MapDriver.newMapDriver(new SomeMapper());
            mapDriver.withInput(new LongWritable(0), new Text("some input"))
                .withMultiOutput(SomeMapper.SUCCESS_FILE, NullWritable.get(), new Text("some result"))
                .runTest();
        }
    }
    

    和 build.gradle

    apply plugin: "java"
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    repositories {
      mavenCentral()
    }
    
    dependencies {
      compile "org.apache.hadoop:hadoop-client:2.4.0"
      testCompile "junit:junit:4.12"
      testCompile("org.apache.mrunit:mrunit:1.1.0:hadoop2") {
        exclude(group: "org.mockito")
      }
      testCompile "org.powermock:powermock-module-junit4:1.6.2"
      testCompile "org.powermock:powermock-api-mockito:1.6.2"
    }
    

    注意 Mockito 排除项。没有它,我得到了异常 java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.getSerializableMode()Lorg/mockito/mock/SerializableMode;,因为 Hadoop 依赖项引入了 org.mockito:mockito-core:1.9.5,这与 Powermock 想要使用的 Mockito 版本冲突。

    您可以在 MRUnit 的 org.apache.hadoop.mrunit.mapreduce.TestMultipleOutput 单元测试中找到更多示例。

    【讨论】:

    • 我正在使用 Maven。当我尝试使用 PowerMock 时出现以下错误。我也排除了 mockito 依赖。
    • 我正在使用 Maven。当我尝试使用 PowerMock 时出现以下错误。我也排除了 mockito 依赖。 java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/Enhancer at org.powermock.api.extension.proxyframework.ProxyFrameworkImpl.isProxy(ProxyFrameworkImpl.java:50)&lt;dependency&gt; &lt;groupId&gt;org.apache.mrunit&lt;/groupId&gt; &lt;artifactId&gt;mrunit&lt;/artifactId&gt; &lt;version&gt;1.1.0&lt;/version&gt; &lt;classifier&gt;hadoop2&lt;/classifier&gt; &lt;exclusions&gt; &lt;exclusion&gt; &lt;groupId&gt;org.mockito&lt;/groupId&gt; &lt;artifactId&gt;mockito-core&lt;/artifactId&gt; &lt;/exclusion&gt; &lt;/exclusions&gt; &lt;/dependency&gt;
    • 您的依赖版本是否与我的示例中的版本不同?显然mvn dependency:tree 可能会提供一些见解。听起来你的 Powermock 版本不包含 Mockito。
    • 我将答案编辑为使用 EMR 的最高 Hadoop 版本 supported,因为您将问题标记为 EMR。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多