Advertisement
Guest User

WordCount.java

a guest
Mar 10th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.hadoop.examples;
  19.  
  20. import java.io.IOException;
  21. import java.util.StringTokenizer;
  22.  
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.fs.Path;
  25. import org.apache.hadoop.io.IntWritable;
  26. import org.apache.hadoop.io.Text;
  27. import org.apache.hadoop.mapreduce.Job;
  28. import org.apache.hadoop.mapreduce.Mapper;
  29. import org.apache.hadoop.mapreduce.Partitioner;
  30. import org.apache.hadoop.mapreduce.Reducer;
  31. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  32. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  33. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  34. import org.apache.hadoop.util.GenericOptionsParser;
  35.  
  36. public class WordCount {
  37.  
  38.   public static class TokenizerMapper
  39.        extends Mapper<Object, Text, Text, IntWritable>{
  40.    
  41.     private final static IntWritable one = new IntWritable(1);
  42.     private Text word = new Text();
  43.      
  44.     public void map(Object key, Text value, Context context
  45.                     ) throws IOException, InterruptedException {
  46.       //Using existing StringTokenizer, just added the punctuation I wanted to ignore
  47.       StringTokenizer itr = new StringTokenizer(value.toString(), "\t\n\r\f,.;:?!()[]#’%'");  
  48.       //Getting the filename, then adding it to the key (which in this case is word)
  49.       FileSplit split = (FileSplit) context.getInputSplit();
  50.       String filename = split.getPath().getName().toString();
  51.       while (itr.hasMoreTokens()) {
  52.         word.set(filename+":"+itr.nextToken());
  53.         context.write(word, one);
  54.       }
  55.     }
  56.   }
  57.  
  58.   //Bonus Question
  59.   public static class WordCountPartitioner extends Partitioner<Text,IntWritable> {
  60.       public int getPartition(Text key, IntWritable value, int numPartitions) {
  61.           char fl = (char)key.getBytes()[0];
  62.           int letter = Character.getNumericValue(Character.toString(fl).toLowerCase().charAt(0))-97;
  63.           int t = 26/numPartitions;
  64.          
  65.           return letter%t;
  66.       }
  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.       int sum = 0;
  77.       for (IntWritable val : values) {
  78.         sum += val.get();
  79.       }
  80.       result.set(sum);
  81.       context.write(key, result);
  82.     }
  83.   }
  84.  
  85.   public static void main(String[] args) throws Exception {
  86.     Configuration conf = new Configuration();
  87.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  88.     if (otherArgs.length != 2) {
  89.       System.err.println("Usage: wordcount <in> <out>");
  90.       System.exit(2);
  91.     }
  92.     Job job = new Job(conf, "word count");
  93.     job.setJarByClass(WordCount.class);
  94.     job.setMapperClass(TokenizerMapper.class);
  95.     job.setCombinerClass(IntSumReducer.class);
  96.     job.setReducerClass(IntSumReducer.class);
  97.     job.setPartitionerClass(WordCountPartitioner.class);
  98.     job.setOutputKeyClass(Text.class);
  99.     job.setOutputValueClass(IntWritable.class);
  100.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  101.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  102.     System.exit(job.waitForCompletion(true) ? 0 : 1);
  103.    
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement