FancyKing

第1关:WordCount词频统计 EduCoder

Mar 19th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3.  
  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.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.util.GenericOptionsParser;
  14.  
  15. /**
  16.  * @author FancyKing
  17.  */
  18. public class WordCount {
  19.     /*
  20.     * MapReduceBase类:实现Mapper和Reducer接口的基类    
  21.     * Mapper接口:
  22.     * WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类要实现此接口。    
  23.     */  
  24.   public static class TokenizerMapper
  25.        extends Mapper<Object, Text, Text, IntWritable>{
  26.     /*
  27.     *LongWritable,IntWritable,Text是Hadoop中实现的用于封装Java数据类型的类,这些类实现了WritableComparable接口,
  28.     *都能够被串行化,便于在分布式环境中进行数据交换,可以视为long,int,String数据类型的替代。
  29.     */
  30.     private final static IntWritable one = new IntWritable(1);
  31.     private Text word = new Text();//Text实现了BinaryComparable类,可以作为key值
  32.  
  33.     /*
  34.     * Mapper接口中的map方法:
  35.     * void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter)
  36.     * 映射一个单个的输入<K1,V1>对到一个中间输出<K2,V2>对
  37.     * 中间输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。    
  38.     * OutputCollector接口:收集Mapper和Reducer输出的<K,V>对。
  39.     * OutputCollector接口的collect(k, v)方法:增加一个(k,v)对到output
  40.     * Reporter 用于报告整个应用的运行进度
  41.      */  
  42.  
  43.     public void map(Object key, Text value, Context context
  44.                     ) throws IOException, InterruptedException {
  45.     /*
  46.          * 原始数据(以test1.txt为例):
  47.      *  tale as old as time
  48.         true as it can be
  49.         beauty and the beast
  50.         map阶段,数据如下形式作为map的输入值:key为偏移量
  51.             <0  tale as old as time>
  52.             <21 world java hello>
  53.             <39 you me too>
  54.          */
  55.          
  56.          /**
  57.        * 解析(Spliting)后以得到键值对<K2,V2>(仅以test1.txt为例)
  58.          * 格式如下:前者是键值,后者数字是值
  59.          * tale 1
  60.          * as 1
  61.          * old 1
  62.          * as 1
  63.          * time 1
  64.          * true 1
  65.          * as 1
  66.          * it 1
  67.          * can 1
  68.          * be 1
  69.          * beauty 1
  70.          * and 1
  71.          * the 1
  72.          * beast 1
  73.          * 这些键值对作为map的输出数据
  74.          */
  75.  
  76.     //****请补全map函数内容****//
  77.     /*********begin*********/
  78.  
  79.     StringTokenizer stringTokenizer = new StringTokenizer(value.toString());
  80.       while (stringTokenizer.hasMoreTokens()) {
  81.         word.set(stringTokenizer.nextToken());
  82.         context.write(word, one);
  83.       }
  84.  
  85.  
  86.  
  87.     /*********end**********/
  88.  
  89.     }
  90.   }
  91.  
  92.   public static class IntSumReducer
  93.        extends Reducer<Text,IntWritable,Text,IntWritable> {
  94.     private IntWritable result = new IntWritable();
  95.  
  96.      /*
  97.      * reduce过程是对输入键值对洗牌(Shuffing)形成<K2,list(V2)>格式数据(仅以test1.txt为例):
  98.      * (tablie [1])
  99.      * (as [1,1,1])
  100.      * (old [1])
  101.      * (time [1])
  102.      * (true [1])
  103.      * (it [1])
  104.      * (can [1])
  105.      * (be [1])
  106.      * (beauty [1])
  107.      * (and [1])
  108.      * (the [1])
  109.      * (beast [1])
  110.      * 作为reduce的输入
  111.      *
  112.      */
  113.     public void reduce(Text key, Iterable<IntWritable> values,
  114.                        Context context
  115.                        ) throws IOException, InterruptedException {
  116.     //****请补全reduce对<k2, list(v2)> 进行合计得到list(<k3,v3>)过程****//
  117.     /*********begin*********/
  118.  
  119.     int sum = 0;
  120.     for (IntWritable val : values) {
  121.       sum += val.get();
  122.     }
  123.  
  124.  
  125.  
  126.     /*********end**********/
  127.  
  128.     //****请将list(<k3,v3>)统计输出****//
  129.  
  130.     /*********begin*********/
  131.  
  132.     result.set(sum);
  133.     context.write(key, result);
  134.  
  135.  
  136.  
  137.     /*********end**********/
  138.     }
  139. }
  140.   public static void main(String[] args) throws Exception {
  141.       /**
  142.        * JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作
  143.        * 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等
  144.        */  
  145.     Configuration conf = new Configuration();
  146.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  147.     /*
  148.     * 需要配置输入和输出的HDFS的文件路径参数
  149.     * 可以使用"Usage: wordcount <in> <out>"实现程序运行时动态指定输入输出
  150.     */
  151.     if (otherArgs.length != 2) {
  152.         System.err.println("Usage: wordcount <in> <out>");
  153.         System.exit(2);
  154.     }
  155.     Job job = new Job(conf, "word count");//Job(Configuration conf,String jobName)设置job名称
  156.     job.setJarByClass(WordCount.class);//为job设置Mapper类
  157.       /*********begin*********/
  158.       //****请为job设置Mapper类****//
  159.     job.setMapperClass(TokenizerMapper.class);
  160.     job.setCombinerClass(IntSumReducer.class);//为job设置Combiner类
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.       //****请为job设置Reduce类****//
  169.     job.setReducerClass(IntSumReducer.class);
  170.       //****请设置输出key的参数类型****//
  171.     job.setOutputKeyClass(Text.class);
  172.  
  173.       //****请设置输出value的类型****//
  174.     job.setOutputValueClass(IntWritable.class);
  175.  
  176.       /*********end**********/
  177.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//为map-reduce任务设置InputFormat实现类,设置输入路径
  178.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为map-reduce任务设置OutputFormat实现类,设置输出路径
  179.     System.exit(job.waitForCompletion(true) ? 0 : 1);
  180.   }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment