Hadoop WordCount解读

因为最近配置了Hadoop的伪分布式和Hbase和Zookeepr的集群环境。现在正准备研究Hadooop,今天第一个Map/Reduce,启动成功,成就啊。
Java代码   收藏代码
  1. package org.frame.base.hbase.hadoop;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.util.GenericOptionsParser;  
  16.   
  17. public class WordCount {  
  18.   
  19.   /** 
  20.    * TokenizerMapper 继续自 Mapper<Object, Text, Text, IntWritable> 
  21.    * 
  22.    * [一个文件就一个map,两个文件就会有两个map] 
  23.    * map[这里读入输入文件内容 以" \t\n\r\f" 进行分割,然后设置 word ==> one 的key/value对] 
  24.    * 
  25.    * @param Object  Input key Type: 
  26.    * @param Text    Input value Type: 
  27.    * @param Text    Output key Type: 
  28.    * @param IntWritable Output value Type: 
  29.    * 
  30.    * Writable的主要特点是它使得Hadoop框架知道对一个Writable类型的对象怎样进行serialize以及deserialize. 
  31.    * WritableComparable在Writable的基础上增加了compareT接口,使得Hadoop框架知道怎样对WritableComparable类型的对象进行排序。 
  32.    * 
  33.    * @author yangchunlong.tw 
  34.    * 
  35.    */  
  36.   public static class TokenizerMapper  
  37.        extends Mapper<Object, Text, Text, IntWritable>{  
  38.   
  39.     private final static IntWritable one = new IntWritable(1);  
  40.     private Text word = new Text();  
  41.     public void map(Object key, Text value, Context context  
  42.                     ) throws IOException, InterruptedException {  
  43.       StringTokenizer itr = new StringTokenizer(value.toString());  
  44.       while (itr.hasMoreTokens()) {  
  45.         word.set(itr.nextToken());  
  46.         context.write(word, one);  
  47.       }  
  48.     }  
  49.   }  
  50.   
  51.   /** 
  52.    * IntSumReducer 继承自 Reducer<Text,IntWritable,Text,IntWritable> 
  53.    * 
  54.    * [不管几个Map,都只有一个Reduce,这是一个汇总] 
  55.    * reduce[循环所有的map值,把word ==> one 的key/value对进行汇总] 
  56.    * 
  57.    * 这里的key为Mapper设置的word[每一个key/value都会有一次reduce] 
  58.    * 
  59.    * 当循环结束后,最后的确context就是最后的结果. 
  60.    * 
  61.    * @author yangchunlong.tw 
  62.    * 
  63.    */  
  64.   public static class IntSumReducer  
  65.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  66.     private IntWritable result = new IntWritable();  
  67.   
  68.     public void reduce(Text key, Iterable<IntWritable> values,  
  69.                        Context context  
  70.                        ) throws IOException, InterruptedException {  
  71.       int sum = 0;  
  72.       for (IntWritable val : values) {  
  73.         sum += val.get();  
  74.       }  
  75.       result.set(sum);  
  76.       context.write(key, result);  
  77.     }  
  78.   }  
  79.   
  80.   public static void main(String[] args) throws Exception {  
  81.     Configuration conf = new Configuration();  
  82.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  83.     /** 
  84.      * 这里必须有输入/输出 
  85.      */  
  86.     if (otherArgs.length != 2) {  
  87.       System.err.println("Usage: wordcount <in> <out>");  
  88.       System.exit(2);  
  89.     }  
  90.     Job job = new Job(conf, "word count");  
  91.     job.setJarByClass(WordCount.class);//主类  
  92.     job.setMapperClass(TokenizerMapper.class);//mapper  
  93.     job.setCombinerClass(IntSumReducer.class);//作业合成类  
  94.     job.setReducerClass(IntSumReducer.class);//reducer  
  95.     job.setOutputKeyClass(Text.class);//设置作业输出数据的关键类  
  96.     job.setOutputValueClass(IntWritable.class);//设置作业输出值类  
  97.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//文件输入  
  98.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//文件输出  
  99.     System.exit(job.waitForCompletion(true) ? 0 : 1);//等待完成退出.  
  100.   }  
  101. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值