Hadoop WordCount 小析

word count 是hadoop的一个经典例子程序,代码如下:
 
Test.java:
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 Test{
 
  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 for the job
    Configuration conf = new Configuration();
    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/core-site.xml"));
    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml"));
  
 
    Job job = new Job(conf, "word count");
    job.setJarByClass(Test.class);
    
    //set the mapper, combiner, reducer
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    
    //the key is type of Text
    job.setOutputKeyClass(Text.class);
    //the value is type of IntWritable
    job.setOutputValueClass(IntWritable.class);
    
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    otherArgs = new String[2];
    otherArgs[0]="/input";
    otherArgs[1]="/output";
    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.out.println("Hello World");
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

 

 
其中,输入的文件有两个:
test1.txt: Hello World Hello
test2.txt: Hello Hadoop
 
例子中通过三个过程最终的出了结果: map,combine和reduce
1 map函数
  map函数使用StringTokenizer来分割单词,遍历每个单词,生成key,value对,比如对于test1.txt文件,map后得到的keyvalue对(三对)为<Hello,1>,<world,1>,<Hello,1>
 
2 combine
  combiner作为本地reducer只接收到了map的本地输出,而来自不同文件的test1.txt和test2.txt不会由同一个map输出到combiner,combine的步骤是为了节省空间。
  比如,test1.txt的map的结果经过combiner,得到的结果是(两对):<Hello, 2>,<world, 1>。
 
3 reduce函数
  reduce函数接收到了combiner的结果,做最后的处理,最终得到<Hello,3>,<world,1>,<Hadoop,1>
 
例子中的Mapper类:TokenizerMapper继承了Mapper<KIN, VIN, KOUT, VOUT>。Mapper子类必须实现void map(K, V, Context)方法,每处理一个<K, V>,交由Context类来处理,一般情况下有Context.write()来写结果。
例子中的Reducer类:IntSumReducer继承了Reducer<KIN, VIN, KOUT, VOUT>。Reducer子类必须实现void reduce(K, V, Iterable<V> values, Context context)方法,每处理一个<K, List of V>,交由Context类来处理,一般情况下有Context.write()来写结果。
 
最终得到结果:
Hello 3
world 1
Hadoop 1
posted @ 2014-03-24 23:10  Rambot  阅读(272)  评论(0编辑  收藏  举报