【问题标题】:Using MultipleOutputs without context.write results empty files在没有 context.write 的情况下使用 MultipleOutputs 结果为空文件
【发布时间】:2017-04-13 21:19:49
【问题描述】:

我不知道如何使用 MultipleOutputs 类。我用它来创建多个输出文件。以下是我的 Driver 类的代码 sn-p

    Configuration conf = new Configuration();

    Job job = Job.getInstance(conf);
    job.setJarByClass(CustomKeyValueTest.class);//class with mapper and reducer
    job.setOutputKeyClass(CustomKey.class);
    job.setOutputValueClass(Text.class);
    job.setMapOutputKeyClass(CustomKey.class);
    job.setMapOutputValueClass(CustomValue.class);
    job.setMapperClass(CustomKeyValueTestMapper.class);
    job.setReducerClass(CustomKeyValueTestReducer.class);
    job.setInputFormatClass(TextInputFormat.class);

    Path in = new Path(args[1]);
    Path out = new Path(args[2]);
    out.getFileSystem(conf).delete(out, true);

    FileInputFormat.setInputPaths(job, in);
    FileOutputFormat.setOutputPath(job, out);

    MultipleOutputs.addNamedOutput(job, "islnd" , TextOutputFormat.class, CustomKey.class, Text.class);
    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
    MultipleOutputs.setCountersEnabled(job, true);

    boolean status = job.waitForCompletion(true);

在 Reducer 中,我使用了这样的 MultipleOutputs,

private MultipleOutputs<CustomKey, Text> multipleOutputs;

@Override
public void setup(Context context) throws IOException, InterruptedException {
    multipleOutputs = new MultipleOutputs<>(context);
}

@Override
public void reduce(CustomKey key, Iterable<CustomValue> values, Context context) throws IOException, InterruptedException {
    ...
     multipleOutputs.write("islnd", key, pop, key.toString());
    //context.write(key, pop);

}

public void cleanup() throws IOException, InterruptedException {
    multipleOutputs.close();
}

}

当我使用 context.write 时,我会得到包含数据的输出文件。但是当我删除 context.write 时,输出文件是空的。但我不想调用 context.write,因为它会创建额外的文件 part-r-00000。正如here(类描述中的最后一段)所述,我使用 LazyOutputFormat 来避免使用 part-r-00000 文件。但是还是不行。

【问题讨论】:

    标签: java hadoop mapreduce


    【解决方案1】:

    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);

    这意味着,如果您没有创建任何输出,请不要创建空文件。

    Can you please look at hadoop counters and find 
     1. map.output.records
     2. reduce.input.groups
     3. reduce.input.records  to verify if your mappers are sending any data to mapper.
    

    多输出的 IT 代码是 http://bytepadding.com/big-data/map-reduce/multipleoutputs-in-map-reduce/

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 2020-12-07
      相关资源
      最近更新 更多