【问题标题】:Why can this map reduce program use an unqualified `Context` without import it?为什么这个 map reduce 程序可以在不导入的情况下使用不合格的 `Context`?
【发布时间】:2019-05-22 01:29:45
【问题描述】:

我正在研究来自https://hadoop.apache.org/docs/r3.1.2/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Example:_WordCount_v1.0 的 Hadoop MapReduce 示例

程序中的Context是指https://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/apidocs/org/apache/hadoop/mapreduce/Mapper.Context.html吗?

如果是,为什么它可以使用不合格的Context 而没有import org.apache.hadoop.mapreduce.Mapper.Context

谢谢。

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }
  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();
    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }
  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

【问题讨论】:

    标签: java hadoop


    【解决方案1】:

    注意Context 的全类名是怎样的

    org.apache.hadoop.mapreduce.Mapper.Context
    

    ContextMapper 中的一个内部类。因为WordCount 类扩展了Mapper&lt;Object, Text, Text, IntWritable&gt; 并且Mapper 类包含内部类Context,所以WordCount 类可以直接访问Context 类而无需导入它。它已经导入 Mapper 扩展它。通常仅导入它不足以让您访问内部类,但这里的不同之处在于扩展 Mapper

    【讨论】:

    • 谢谢。为什么Context不是由Mapper.Context访问,而是由Context访问?
    • 因为WordCount是Mapper的子类,所以可以直接访问内部类,不需要限定。
    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 2020-04-21
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多