Hadoop WordCount详细分析

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
 {
      整型:IntWritable, 这是Hadoop对int的封装
       字符串型:Text,这是Hadoop对String的封装
       LongWritable long的类型封装
  public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> 
 {
    private final IntWritable one = new IntWritable(1); new一个int类型  用来计数,这里是1,
    private Text word = new Text();  new一个string类型   这是定义两个数据类型
      
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
    {
      key值为该行的首字符相对于文本文件首字符的偏移量
      value值存储的是文本文件中的一行
      Context,上下文对象,它用来与MapReduce系统进行通信,如把map的结果传给reduce处理
      StringTokenizer itr = new StringTokenizer(value.toString());  StringTokenizer类是将每一行拆分为一个个的单词
      while (itr.hasMoreTokens())  用来判断当前还有没有可以遍历的元素 遍历一下每行字符串中的单词
      {
        word.set(itr.nextToken());//打印下一个字段或者元素 //出现一个单词就给它设成一个key并将其值设为1
        context.write(word, one); 增加一个(k,v)对到context 用context.write(key,value)来传递数据
        one这里是1,所以,输出的(key,value)->(word, one)都是这样的形式:(“单词”,1)。可供之后处理。
        //输出设成的key/value值
      }
    }
  }
  
  public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> 
{
    private IntWritable result = new IntWritable();
    reducer将中间输出键值对中那些键相同的合并(内部已经把上面map阶段相同的key进行了合并),值为集合。一个key对应一组value
    在reduce阶段,每个key传进来,reduce方法都被调用一次
    传进来的键值对的形式是<key,>,即value不是一个值,而是值的集合。用一个for循环,即可遍历某个key里面的所有值:
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
  {
      int sum = 0;
      这个for循环将某个key对应的所有的value累加,即某单词出现次数的累加
      /由于map的打散,这里会得到如,{key,values}={"hello",{1,1,....}},这样的集合

      for (IntWritable val : values) 
        {
          sum += val.get(); //这里需要逐一将它们的value取出来予以相加,取得总的出现次数,即为汇和
         }
      result.set(sum);
      context.write(key, result); context.write(key,result)就把某单词出现的次数输出了 result是单词的次数
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    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);
  }
----------------------------------------------------------------------------------------------------------
  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");      
//此程序的执行,在hadoop看来是一个Job,故进行初始化job操作
    job.setJarByClass(Wordcount.class);        
 //可以认为成,此程序要执行MyWordCount.class这个字节码文件
    job.setMapperClass(TokenizerMapper.class); 
//在这个job中,我用TokenizerMapper这个类的map函数
    job.setCombinerClass(IntSumReducer.class);  
    job.setReducerClass(IntSumReducer.class);   
//在这个job中,我用IntSumReducer这个类的reduce函数 
    job.setOutputKeyClass(Text.class);          
//在reduce的输出时,key的输出类型为Text
    job.setOutputValueClass(IntWritable.class);  
//在reduce的输出时,value的输出类型为IntWritable
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
//初始化要计算word的文件的路径
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
//初始化要计算word的文件的之后的结果的输出路径 
    System.exit(job.waitForCompletion(true) ? 0 : 1);
 //提交job到hadoop上去执行了,意思是指如果这个job真正的执行完了则主函数退出了,若没有真正的执行完就退出了。  
  } 
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值