title: Map reduce to produce n most frequent words
body: <p>I have MapReduce code which prints the amount of times a word was seen in a document.  I would like to change this code to produce the N most frequent words. N being an input parameter.  Here is the code:</p>
  
  <pre><code>    import java.io.IOException;        
      import org.apache.hadoop.conf.Configuration;       
      import org.apache.hadoop.fs.Path;
      import org.apache.hadoop.io.IntWritable;        
      import org.apache.hadoop.io.LongWritable;        
      import org.apache.hadoop.io.Text;        
      import org.apache.hadoop.mapreduce.Job;        
      import org.apache.hadoop.mapreduce.Mapper;        
      import org.apache.hadoop.mapreduce.Reducer;        
      import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;        
      import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;        
      import org.apache.hadoop.util.GenericOptionsParser;
  
      public class WordCount {
  
      public static void main(String [] args) throws Exception
  
      {
  
      Configuration c=new Configuration();        
      String[] files=new GenericOptionsParser(c,args).getRemainingArgs();        
      Path input=new Path(files[0]);        
      Path output=new Path(files[1]);        
      Job j=new Job(c,"wordcount");        
      j.setJarByClass(WordCount.class);        
      j.setMapperClass(MapForWordCount.class);        
      j.setReducerClass(ReduceForWordCount.class);        
      j.setOutputKeyClass(Text.class);        
      j.setOutputValueClass(IntWritable.class);        
      FileInputFormat.addInputPath(j, input);        
      FileOutputFormat.setOutputPath(j, output);        
      System.exit(j.waitForCompletion(true)?0:1);
  
      }
  
      public static class MapForWordCount extends Mapper&lt;LongWritable, Text, Text, IntWritable&gt;{
  
      public void map(LongWritable key, Text value, Context con) throws IOException, InterruptedException
  
      {
  
      String line = value.toString();
  
      String[] words=line.split("[^\\p{L}0-9]+");
  
      for(String word: words )
  
      {
           if(word.length()&lt;=3){
               continue;
           }else{
               Text outputKey = new Text(word.toUpperCase().trim());
  
                IntWritable outputValue = new IntWritable(1);
  
                con.write(outputKey, outputValue); 
           }
            } } }
  
      public static class ReduceForWordCount extends Reducer&lt;Text, IntWritable, Text, IntWritable&gt;
      {
  
   public void reduce(Text word, Iterable&lt;IntWritable&gt; values, Context con) throws IOException, InterruptedException
  
      {
  
      int sum = 0;
  
         for(IntWritable value : values)
  
         {
  
         sum += value.get();
  
         }
  
         con.write(word, new IntWritable(sum));
  
      }
  
      }
  
      }
  </code></pre>
  
  <p>I use a file in the hadoop file system to compute the word count and right now I just   get a list of words with the amount of times they occur in the text.  </p>