title: Mapreduce Word Count Hadoop Highest Frequency Word
body: <p>So from the Hadoop tutorial website (<a href="http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code" rel="nofollow">http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code</a>) on how to implement word count using a map reduce approach I understand how it works and that the output will be all words with there frequency. </p>
  
  <p>What I want to do is only have the output be the highest frequency word from the Input file I have. </p>
  
  <p><code>Example:
  Jim
  Jim
  Jim
  Jim
  Tom
  Dane</code></p>
  
  <p>I want the output just to be</p>
  
  <pre><code>Jim 4
  </code></pre>
  
  <p>The current Output from Word count is each word and it's frequency. Have anyone edited the Word count so that it just prints the highest frequency word and its frequency?</p>
  
  <p>Does anyone have any tips on how to achieve this?</p>
  
  <p>How would I write another MapReducer that will find the highest frequency word from the Output of WordCount?</p>
  
  <p>Or is there another way?</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thank you!</p>
  
  <p>WordCount.jave:</p>
  
  <pre><code>import java.io.IOException;
  import java.util.StringTokenizer;
  
  import org.apache.hadoop.conf.Configuration;
  import org.apache.hadoop.fs.Path;
  import org.apache.hadoop.io.IntWritable;
  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;
  
  public class WordCount {
  
    public static class TokenizerMapper
         extends Mapper&lt;Object, Text, Text, IntWritable&gt;{
  
      private final static IntWritable one = new IntWritable(1);
      private Text word = new Text();
  
      public void map(Object key, Text value, Context context
                      ) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
          word.set(itr.nextToken());
          context.write(word, one);
        }
      }
    }
  
    public static class IntSumReducer
         extends Reducer&lt;Text,IntWritable,Text,IntWritable&gt; {
      private IntWritable result = new IntWritable();
  
      public void reduce(Text key, Iterable&lt;IntWritable&gt; values,
                         Context context
                         ) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
          sum += val.get();
        }
        result.set(sum);
        context.write(key, result);
      }
    }
  
    public static void main(String[] args) throws Exception {
      Configuration conf = new Configuration();
      Job job = Job.getInstance(conf, "word count");
      job.setJarByClass(WordCount.class);
      job.setMapperClass(TokenizerMapper.class);
      job.setCombinerClass(IntSumReducer.class);
      job.setReducerClass(IntSumReducer.class);
      job.setOutputKeyClass(Text.class);
      job.setOutputValueClass(IntWritable.class);
      FileInputFormat.addInputPath(job, new Path(args[0]));
      FileOutputFormat.setOutputPath(job, new Path(args[1]));
      System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
  }
  </code></pre>