映射减少计数示例

2024-04-17

我的问题是关于mapreduce programming in java.

假设我有 WordCount.java 示例,一个标准mapreduce program。我希望map函数收集一些信息,并返回形成如下的reduce函数map:<slaveNode_id,some_info_collected>,

so that I can know what slave node collected what data..知道怎么做吗?

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);
    }
}

谢谢你!!


您所要求的是让应用程序(您的地图缩减事物)了解它运行的基础设施。

一般来说,答案是您的应用程序不需要此信息。对Mapper 的每次调用和对Reducer 的每次调用都可以在不同的节点上执行,也可以全部在同一节点上执行。 MapReduce 的美妙之处在于结果是相同的,因此对于您的应用程序:这并不重要。

因此,API 没有功能来支持您的此请求。

祝你学习 Hadoop 愉快:)


附:我能想到的唯一方法(至少可以说是令人讨厌的)是在映射器中包含某种类型的系统调用,并向底层操作系统询问它的名称/属性/等。 这种构造将使您的应用程序非常不可移植;即它不会在 Windows 或 Amazon 的 Hadoop 上运行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

映射减少计数示例 的相关文章

随机推荐