【问题标题】:Hadoop: How does OutputCollector work during MapReduce?Hadoop:在 MapReduce 期间 OutputCollector 如何工作?
【发布时间】:2012-06-12 12:46:52
【问题描述】:

我想知道 Map 函数中是否使用了 OutputCollector 的“实例”输出: output.collect(键,值) 这个 - 输出 - 将键值对存储在某处? 即使它发送到reducer函数,它们也必须是一个中间文件,对吧? 那些文件是什么?它们是否可见并由程序员决定? 我们在 main 函数中指定的 OutputKeyClass 和 OutputValueClasses 是这些存储的地方吗? [Text.class 和 IntWritable.class]

我给出了 MapReduce 中 Word Count 示例的标准代码,我们可以在网络的许多地方找到它。

public class WordCount {

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}

public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));    
JobClient.runJob(conf);
}
}

【问题讨论】:

  • 为什么要访问这些临时文件?您是否有想要实现的特定目标?还是只是好奇?
  • 我想更改这些临时文件的位置。

标签: java hadoop mapreduce


【解决方案1】:

Map 函数的输出存储在临时中间文件中。这些文件由 Hadoop 透明地处理,因此在正常情况下,程序员无权访问它。如果您对每个映射器内部发生的事情感到好奇,您可以查看相应作业的日志,您可以在其中找到每个映射任务的日志文件。

如果您想控制临时文件的生成位置并访问它们,您必须创建自己的 OutputCollector 类,我不知道这有多容易。

如果想看源码,可以使用svn来获取。我认为它可以在这里找到:http://hadoop.apache.org/common/version_control.html

【讨论】:

    【解决方案2】:

    我相信它们存储在临时位置并且对开发人员不可用,除非您创建自己的类来实现OutputCollector

    我曾经不得不访问这些文件并通过创建副作用文件来解决问题: http://hadoop.apache.org/common/docs/r0.20.2/mapred_tutorial.html#Task+Side-Effect+Files

    【讨论】:

    • 谁有 OutputCollector 的 .collect() 函数的代码?
    【解决方案3】:

    中间的、分组的输出总是存储在 SequenceFiles 中。应用程序可以通过 JobConf 指定是否以及如何压缩中间输出以及使用哪些 CompressionCodecs。

    http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapred/Mapper.html

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多