Java 你能一步一步地解释一下单词计数mapreduce程序吗

Java 你能一步一步地解释一下单词计数mapreduce程序吗,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,你能解释一下地图缩小程序吗。例如,在word count程序中,类中的类是innerclass。你能一步一步地解释一下程序吗。角括号是什么意思。我们为什么还要编写输出参数。什么是上下文对象。这样你就可以一步一步地解释程序了。我懂逻辑,但很少懂Java语句 public class WordCount { public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private

你能解释一下地图缩小程序吗。例如,在word count程序中,类中的类是innerclass。你能一步一步地解释一下程序吗。角括号是什么意思。我们为什么还要编写输出参数。什么是上下文对象。这样你就可以一步一步地解释程序了。我懂逻辑,但很少懂Java语句

public class WordCount {

public static class Map extends 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, Context context) throws IOException, InterruptedException {
       String line = value.toString();
       StringTokenizer tokenizer = new StringTokenizer(line);
       while (tokenizer.hasMoreTokens()) {
           word.set(tokenizer.nextToken());
           context.write(word, one);
       }
   }
} 

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {

   public void reduce(Text key, Iterable<IntWritable> values, Context context) 
     throws IOException, InterruptedException {
       int sum = 0;
       for (IntWritable val : values) {
           sum += val.get();
       }
       context.write(key, new IntWritable(sum));
   }
}

public static void main(String[] args) throws Exception {
   Configuration conf = new Configuration();

       Job job = new Job(conf, "wordcount");

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

   job.setMapperClass(Map.class);
   job.setReducerClass(Reduce.class);

   job.setInputFormatClass(TextInputFormat.class);
   job.setOutputFormatClass(TextOutputFormat.class);

   FileInputFormat.addInputPath(job, new Path(args[0]));
   FileOutputFormat.setOutputPath(job, new Path(args[1]));

   job.waitForCompletion(true);
}

}
公共类字数{
公共静态类映射扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共void映射(LongWritable键、文本值、上下文上下文)引发IOException、InterruptedException{
字符串行=value.toString();
StringTokenizer标记器=新的StringTokenizer(行);
while(tokenizer.hasMoreTokens()){
set(tokenizer.nextToken());
上下文。写(单词,一);
}
}
} 
公共静态类Reduce扩展Reducer{
公共void reduce(文本键、Iterable值、上下文)
抛出IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=val.get();
}
write(key,newintwriteable(sum));
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
Job Job=新作业(conf,“wordcount”);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
setInputFormatClass(TextInputFormat.class);
setOutputFormatClass(TextOutputFormat.class);
addInputPath(作业,新路径(args[0]);
setOutputPath(作业,新路径(args[1]);
job.waitForCompletion(true);
}
}

您的Map类扩展了Hadoop的Mapper类,其中提到了输入和输出参数的泛型。 前两个参数是输入键值,而后两个参数是输出键值。 Mapper类需要重写map()方法。你的映射逻辑在这里。 此方法接受指定的输入参数并返回void,并将键值对写入上下文(内存)

您的Reduce类扩展了Reducer类。减速器的输入应与映射器/组合器的输出键值匹配。 Reducer类需要重写reduce()方法。你的逻辑在这里。 此方法接受指定的输入参数并返回void,并从上下文(内存)中读取键值对

Hadoop在这两种方法之间执行合并、排序和洗牌操作

主方法包含Hadoop作业的代码设置

没有更多的澄清。 和