wordCount原理+JAVA基础复习

来源:互联网 发布:网络语言暴力的影响 编辑:程序博客网 时间:2024/05/12 05:08
  1. package org.apache.hadoop.examples;  
  2. import java.io.IOException;  
  3. import java.util.StringTokenizer;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapred.JobConf;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  14. import org.apache.hadoop.util.GenericOptionsParser;  
  15.   
  16. public class WordCount {  
  17.   
  18.      /**  
  19.      * MapReduceBase类:实现了Mapper和Reducer接口的基类(其中的方法只是实现接口,而未作任何事情)  
  20.      * Mapper接口:  
  21.      * WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类应该实现此接口。  
  22.      * Reporter 则可用于报告整个应用的运行进度,本例中未使用。   
  23.      *   
  24.      */    
  25.   public static class TokenizerMapper   
  26.        extends Mapper<Object, Text, Text, IntWritable>{  
  27.       
  28.       /**  
  29.        * LongWritable, IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口,  
  30.        * 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为long,int,String 的替代品。  
  31.        */   
  32.     private final static IntWritable one = new IntWritable(1);  
  33.     private Text word = new Text();//Text 实现了BinaryComparable类可以作为key值  
  34.      
  35.       
  36.     /**  
  37.      * Mapper接口中的map方法:  
  38.      * void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter)  
  39.      * 映射一个单个的输入k/v对到一个中间的k/v对  
  40.      * 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。  
  41.      * OutputCollector接口:收集Mapper和Reducer输出的<k,v>对。  
  42.      * OutputCollector接口的collect(k, v)方法:增加一个(k,v)对到output  
  43.      */    
  44.       
  45.     public void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
  46.           
  47.         /** 
  48.          * 原始数据: 
  49.          * c++ java hello 
  50.             world java hello 
  51.             you me too 
  52.             map阶段,数据如下形式作为map的输入值:key为偏移量 
  53.             0  c++ java hello 
  54.             16 world java hello 
  55.             34 you me too 
  56.              
  57.          */  
  58.            
  59.          /** 
  60.           * 以下解析键值对 
  61.          * 解析后以键值对格式形成输出数据 
  62.          * 格式如下:前者是键排好序的,后者数字是值 
  63.          * c++ 1 
  64.          * java 1 
  65.          * hello 1 
  66.          * world 1 
  67.          * java 1 
  68.          * hello 1 
  69.          * you 1 
  70.          * me 1 
  71.          * too 1 
  72.          * 这些数据作为reduce的输出数据 
  73.          */  
  74.       StringTokenizer itr = new StringTokenizer(value.toString());//得到什么值  
  75.       System.out.println("value什么东西 : "+value.toString());  
  76.       System.out.println("key什么东西 : "+key.toString());  
  77.        
  78.       while (itr.hasMoreTokens()) {  
  79.         word.set(itr.nextToken());  
  80.         
  81.         context.write(word, one);  
  82.       }  
  83.     }  
  84.   }  
  85.     
  86.   public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {  
  87.     private IntWritable result = new IntWritable();  
  88.     /** 
  89.      * reduce过程是对输入数据解析形成如下格式数据: 
  90.      * (c++ [1]) 
  91.      * (java [1,1]) 
  92.      * (hello [1,1]) 
  93.      * (world [1]) 
  94.      * (you [1]) 
  95.      * (me [1]) 
  96.      * (you [1]) 
  97.      * 供接下来的实现的reduce程序分析数据数据 
  98.      *  
  99.      */  
  100.     public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  101.       int sum = 0;  
  102.       /** 
  103.        * 自己的实现的reduce方法分析输入数据 
  104.        * 形成数据格式如下并存储 
  105.        *     c++    1 
  106.        *    hello   2 
  107.        *    java    2 
  108.        *    me      1 
  109.        *    too     1 
  110.        *    world   1 
  111.        *    you     1 
  112.        *     
  113.        */  
  114.       for (IntWritable val : values) {  
  115.         sum += val.get();  
  116.       }  
  117.        
  118.       result.set(sum);  
  119.       context.write(key, result);  
  120.     }  
  121.   }  
  122.   
  123.   public static void main(String[] args) throws Exception {  
  124.         
  125.       /**  
  126.        * JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作  
  127.        * 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等  
  128.        */    
  129.         
  130.     Configuration conf = new Configuration();  
  131.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  132.     //这里需要配置参数即输入和输出的HDFS的文件路径  
  133.     if (otherArgs.length != 2) {  
  134.       System.err.println("Usage: wordcount <in> <out>");  
  135.       System.exit(2);  
  136.     }  
  137.    // JobConf conf1 = new JobConf(WordCount.class);  
  138.     Job job = new Job(conf, "word count");//Job(Configuration conf, String jobName) 设置job名称和  
  139.     job.setJarByClass(WordCount.class);  
  140.     job.setMapperClass(TokenizerMapper.class); //为job设置Mapper类   
  141.     job.setCombinerClass(IntSumReducer.class); //为job设置Combiner类    
  142.     job.setReducerClass(IntSumReducer.class); //为job设置Reduce类     
  143.     job.setOutputKeyClass(Text.class);        //设置输出key的类型  
  144.     job.setOutputValueClass(IntWritable.class);//  设置输出value的类型  
  145.     FileInputFormat.addInputPath(job, new Path(otherArgs[0])); //为map-reduce任务设置InputFormat实现类   设置输入路径  
  146.       
  147.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为map-reduce任务设置OutputFormat实现类  设置输出路径  
  148.     System.exit(job.waitForCompletion(true) ? 0 : 1);  
  149.   }  

参考学习:http://blog.csdn.net/kky2010_110/article/details/7865499
0 0
原创粉丝点击