[Big Data]菜鸟的Hadoop (Before YARN) 学习笔记 (一) WordCount

菜鸟的Hadoop (Before YARN)学习笔记(一)  WordCount

 

配置之后再补。先行略过。之前花了挺多时间在配置,但是手一抖没Mark Down,实在后悔。

 

1. New a project



OK. Finish.

2. Project Structure and Coding

可以看到,其实Mapreduce已经很自动地把要用的JAR等等必要的资源文件放一起了。


那这里会有一个示例的WordCount.

来自Hadoop Source Examples,,可能有不同版本,不过实现原理一致。

 

[java]  view plain  copy
  1. package com.wordCount;  
  2.    
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.    
  6. importorg.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. importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. importorg.apache.hadoop.util.GenericOptionsParser;  
  16.    
  17. public class WordCount {  
  18.    
  19.  public static class TokenizerMapper  
  20.       extends Mapper<Object, Text, Text, IntWritable>{  
  21.     //TokenizerMapper 继承自 Mapper<Object, Text, Text, IntWritable>   
  22. //LongWritable, IntWritable, Text 用于封装数据类型,以便串行化从而便于在分布式环境中进行数据交换,应可理解为long ,int, string 。   
  23. //在MapReduce中,Mapper从一个输入分片中读取数据,然后经过Shuffle and Sort阶段分发数据给Reducer.  
  24.   // Map函数接收一个<key,value>形式的输入,然后同样产生一个<key,value>形式的中间输出,Hadoop函数接收一个如<key,(list of values)>形式的输入,然后对这个value集合进行处理,每个reduce产生0或1个输出,reduce的输出也是<key,value>形式的。  
  25.   //Map类继承自MapReduceBase,并且它实现了Mapper接口,Mapper接口是一个规范类型,它有4种形式的参数,分别用来指定map的输入key值类型、输入value值类型、输出key值类型和输出value值类型。在本例中,因为使用的是TextInputFormat,它的输出key值是LongWritable类型,输出value值是Text类型,所以map的输入类型为<LongWritable,Text>。在本例中需要输出<word,1>这样的形式,因此输出的key值类型是Text,输出的value值类型是IntWritable。  
  26. // 简单说应该是,根据需求定义类型:  
  27. // Object  Input key Type:  
  28. // Text    Input value Type:   
  29.  // Text    Output key Type:   
  30.  // IntWritable Output value Type:   
  31.    
  32.    private final static IntWritable one = new IntWritable(1);  
  33.    private Text word = new Text();  
  34.    
  35. // New one & word.  
  36.        
  37.    public void map(Object key, Text value, Context context  
  38.                     ) throws IOException, InterruptedException{  
  39.      StringTokenizer itr = new StringTokenizer(value.toString());  
  40.      while (itr.hasMoreTokens()) {  
  41.        word.set(itr.nextToken());  
  42.        context.write(word, one);  
  43.      }  
  44.     }  
  45.   }  
  46.    
  47. // 数据对(K/V)是从传入的Context获取的。我们也可以从map方法看出,输出结果K/V对也是通过Context来完成的。 在前期的例子中使用的是void map(LongWritable key, Text value,  OutputCollector<Text, IntWritable> output, Reporter reporter)   
  48. // public StringTokenizer(String str,String delim, boolean returnDelims)  
  49. 第一个参数就是要分隔的String,第二个是分隔字符集合,第三个参数表示分隔符号是否作为标记返回,如果不指定分隔字符,默认的是:”\t\n\r\f”  
  50. // boolean hasMoreTokens() :返回是否还有分隔符  
  51. // String nextToken():返回从当前位置到下一个分隔符的字符串。  
  52. // context.write(key, value);  
  53.    
  54.  public static class IntSumReducer  
  55.       extends Reducer<Text,IntWritable,Text,IntWritable> {  
  56.    private IntWritable result = new IntWritable();  
  57. // New a intWritable 作为结果  
  58.    public void reduce(Text key, Iterable<IntWritable> values,  
  59.                        Context context  
  60.                        ) throws IOException,InterruptedException {  
  61.      int sum = 0;  
  62.      for (IntWritable val : values) {  
  63.        sum += val.get();  
  64.      }  
  65.      result.set(sum);  
  66.      context.write(key, result);  
  67.     }  
  68.   }  
  69.    
  70. // IntSumReducer 继承自 Reducer<Text,IntWritable,Text,IntWritable>   
  71. //参数的理解也是一样的,inputtext/ IntWritable output text/IntWritable  
  72. //重写reduce输出结果,循环所有的map值,把word ==> one 的key/value对进行汇总  
  73. // Map过程输出<key,values>中key为单个单词,而values是对应单词的计数值所组成的列表,Map的输出就是Reduce的输入,所以reduce方法只要遍历values并求和,即可得到某个单词的总次数。  
  74.    
  75.  public static void main(String[] args) throws Exception {  
  76.    Configuration conf = new Configuration();  
  77.    String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();  
  78.    if (otherArgs.length != 2) {  
  79.      System.err.println("Usage: wordcount <in> <out>");  
  80.      System.exit(2);  
  81.     }  
  82.    Job job = new Job(conf, "word count");  
  83.    job.setJarByClass(WordCount.class); //指定主类  
  84.    job.setMapperClass(TokenizerMapper.class);  
  85.    job.setCombinerClass(IntSumReducer.class);  
  86.    job.setReducerClass(IntSumReducer.class);  
  87.    job.setOutputKeyClass(Text.class); //设置输出数据的关键字类   
  88. job.setOutputValueClass(IntWritable.class);  
  89. //以上也是为了设定各自的类  
  90.    FileInputFormat.addInputPath(job, new Path(otherArgs[0])); //文件输入  
  91.    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //文件输出  
  92.    System.exit(job.waitForCompletion(true) ? 0 : 1);  
  93.   }  
  94. }  
  95. //以上是执行。首先是Configurationconf = new Configuration();初始化配置类。  
  96. // GenericOptionsParser是hadoop框架中解析命令行参数的基本类。它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及其他额外的配置资源。  
  97. //getRemainingArgs 限制命令行输入参数,取其数组长度不为2的话报错退出。  
  98. // public String[]getRemainingArgs()  
  99. Returns an array of Strings containing onlyapplication-specific arguments.  
  100. Returns:  
  101. array of Strings containing theun-parsed arguments or empty array if commandLine was not defined.  
  102. //任务的输出和输入路径则由命令行参数指定  
  103. // waitForCompletion开始  


 

三.图解

 

以下摘自虾皮工作室

 http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html

多谢作者的讲解。


  1)将文件拆分成splits,由于测试用的文件较小,所以每个文件为一个split,并将文件按行分割形成<key,value>对,如图4-1所示。这一步由MapReduce框架自动完成,其中偏移量(即key值)包括了回车所占的字符数(Windows和Linux环境会不同)。


图4-1 分割过程

 

2)将分割好的<key,value>对交给用户定义的map方法进行处理,生成新的<key,value>对,如图4-2所示。


图4-2 执行map方法

 

  3)得到map方法输出的<key,value>对后,Mapper会将它们按照key值进行排序,并执行Combine过程,将key至相同value值累加,得到Mapper的最终输出结果。如图4-3所示。


图4-3 Map端排序及Combine过程

 

4)Reducer先对从Mapper接收的数据进行排序,再交由用户自定义的reduce方法进行处理,得到新的<key,value>对,并作为WordCount的输出结果,如图4-4所示。


图4-4 Reduce端排序及输出结果

 

四.运行结果

要通过Run Configurations


输入Arguments.

Run at Hadoop



证明成功了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值