您的位置:首页 > 运维架构

Hadoop 之 Wordcount 单词计数 (学习笔记)

2014-09-09 07:14 465 查看
自从学习了hadoop之后,现在想想还没有怎么整理过自己写过的代码和读过的代码,今天就做一个整理吧,纪念一下。

WordCount (单词计数)程序 就像我们刚刚开始学习程序一样,写一个 helloworld程序一样。

1.问题描述

输入一个文件 word.txt:

xing xing

love you

hello world

hello hadoop
输出结果为:

xing       2
love       1
you        1
hello      2
hadoop 1
word      1
当然单词的输出是有顺序的。

2.设计思路

这个应用实例的解决方案很直接,就是将文件内容切分成单词, 然后将所有相同的单词聚集在一起,然后计算单词出现的次数并输出。 根据MapReduce并行程序设计原则可知,解决方案中的内容切分步骤和数据不相关, 可以并行化处理,每个获得原始数据的机器只要将输入数据切分成单词就可以了。 所以可以在Map阶段完成单词切分任务。另外,相同单词的频数计算也可以井行化处理。由实例要求来看,不同单词之间的频数不相关,所以可以将相同的单词交给一台机器来计算频数, 然后输出最终结果。这个过程可以在Reduce阶段完成。至于将中间结果根据不同单词分组再分发给Reduce机器,
这正好是MapReduce过程中的shuffIe能够完成的,至此,这个实例的MapReduce程序就设计出来了。 Map阶段完成由输入数据到单词切分的工作,shuffle阶段完成相同单词的聚集和分发工作〈这个过程是MapReduce的默认过程,不用具体配置),Reduce阶段负责接收所有单词并计算其频数。MapReduce'中传递的数据都是<key,value>形式的,并且shuffle排序聚集分发都是按照key值进行的,因此将Map的输出设计成由word作为key, 1作为value的形式,这表示单词word出现一次(Map的输入采用Hadoop默认的输入方式:文件的一行作为value,行号作为key)。Reduce的输入为Map输出聚集后的结果,即<key,value-list>,具体到这个实例就是<word,{1,1,1,1....}>,Reduce的输出会设计成与Map输出相同的形式,只是后面的数字不再固定是1,而是具体算出的word所对应的频数。

3.代码如下:

package org.apache.hadoop.examples;

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;
import org.apache.hadoop.util.GenericOptionsParser;

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();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }
    Job job = new Job(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(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: