Hadoop WordCount进阶

Linux大全评论592 views阅读模式
  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. import org.apache.log4j.Logger;   
  15.   
  16. public class WordCount {   
  17.   
  18.   public static Logger loger = Wloger.loger;   
  19.   
  20.   /**  
  21.    * TokenizerMapper 继续自 Mapper<Object, Text, Text, IntWritable>  
  22.    *  
  23.    * [一个文件就一个map,两个文件就会有两个map]  
  24.    * map[这里读入输入文件内容 以" \t\n\r\f" 进行分割,然后设置 word ==> one 的key/value对]  
  25.    *  
  26.    * @param Object  Input key Type:  
  27.    * @param Text    Input value Type:  
  28.    * @param Text    Output key Type:  
  29.    * @param IntWritable Output value Type:  
  30.    *  
  31.    * Writable的主要特点是它使得Hadoop框架知道对一个Writable类型的对象怎样进行serialize以及deserialize.  
  32.    * WritableComparable在Writable的基础上增加了compareT接口,使得Hadoop框架知道怎样对WritableComparable类型的对象进行排序。  
  33.    *  
  34.    * @author yangchunlong.tw  
  35.    *  
  36.    */  
  37.   public static class TokenizerMapper   
  38.        extends Mapper<Object, Text, Text, IntWritable>{   
  39.   
  40.     private final static IntWritable one = new IntWritable(1);   
  41.     private Text word = new Text();   
  42.     public void map(Object key, Text value, Context context   
  43.                     ) throws IOException, InterruptedException {   
  44.       loger.info("Map <key>"+key+"</key>");   
  45.       loger.info("Map <value>"+value+"</key>");   
  46.       StringTokenizer itr = new StringTokenizer(value.toString());   
  47.       while (itr.hasMoreTokens()) {   
  48.         String wordstr = itr.nextToken();   
  49.         word.set(wordstr);   
  50.         loger.info("Map <word>"+wordstr+"</word>");   
  51.         context.write(word, one);   
  52.       }   
  53.     }   
  54.   }   
  55.   
  56.   /**  
  57.    * IntSumReducer 继承自 Reducer<Text,IntWritable,Text,IntWritable>  
  58.    *  
  59.    * [不管几个Map,都只有一个Reduce,这是一个汇总]  
  60.    * reduce[循环所有的map值,把word ==> one 的key/value对进行汇总]  
  61.    *  
  62.    * 这里的key为Mapper设置的word[每一个key/value都会有一次reduce]  
  63.    *  
  64.    * 当循环结束后,最后的确context就是最后的结果.  
  65.    *  
  66.    * @author yangchunlong.tw  
  67.    *  
  68.    */  
  69.   public static class IntSumReducer   
  70.        extends Reducer<Text,IntWritable,Text,IntWritable> {   
  71.     private IntWritable result = new IntWritable();   
  72.   
  73.     public void reduce(Text key, Iterable<IntWritable> values,   
  74.                        Context context   
  75.                        ) throws IOException, InterruptedException {   
  76.       loger.info("Reduce <key>"+key+"</key>");   
  77.       loger.info("Reduce <value>"+values+"</key>");   
  78.       int sum = 0;   
  79.       for (IntWritable val : values) {   
  80.         sum += val.get();   
  81.       }   
  82.       result.set(sum);   
  83.       loger.info("Reduce <sum>"+sum+"</sum>");   
  84.       context.write(key, result);   
  85.     }   
  86.   }   
  87.   
  88.   public static void main(String[] args) throws Exception {   
  89.     Configuration conf = new Configuration();   
  90.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();   
  91.     /**  
  92.      * 这里必须有输入/输出  
  93.      */  
  94.     if (otherArgs.length != 2) {   
  95.       System.err.println("Usage: wordcount <in> <out>");   
  96.       System.exit(2);   
  97.     }   
  98.     Job job = new Job(conf, "word count");   
  99.     job.setJarByClass(WordCount.class);//主类   
  100.     job.setMapperClass(TokenizerMapper.class);//mapper   
  101.     job.setCombinerClass(IntSumReducer.class);//作业合成类   
  102.     job.setReducerClass(IntSumReducer.class);//reducer   
  103.     job.setOutputKeyClass(Text.class);//设置作业输出数据的关键类   
  104.     job.setOutputValueClass(IntWritable.class);//设置作业输出值类   
  105.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//文件输入   
  106.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//文件输出   
  107.     System.exit(job.waitForCompletion(true) ? 0 : 1);//等待完成退出.   
  108.   }   
  109. }  

企鹅博客
  • 本文由 发表于 2019年9月30日 12:51:56
  • 转载请务必保留本文链接:https://www.qieseo.com/148509.html

发表评论