title: Working of Combiner and Reducer class in hadoop
body: <p>I have data for the scores of students of a particular class in the following format </p>
  
  <pre><code>StudentId@CourseId@Marks1@Marks2@Marks3@Marks4@Marks5
  
  01001@104@13@18@25@86@23
  01001@106@58@30@10@12@59
  01001@108@60@81@97@83@26
  01001@110@68@95@11@73@63
  01001@112@55@22@74@71@22
  01002@104@20@72@76@28@99
  01002@106@52@17@20@67@91
  01002@108@18@46@61@73@14
  01002@110@86@59@50@35@65
  01002@112@45@76@97@37@17
  .......
  </code></pre>
  
  <p>I want to compute the average of the scores obtained by a student in 5 different courses (Given By 5 Different Course IDs) in 5 different exams.</p>
  
  <p>My Map and Reduce Classes for the above are as follows :</p>
  
  <pre><code>public static class Map extends MapReduceBase implements Mapper&lt;LongWritable,Text,Text,DoubleWritable&gt;{
      private Text SID=new Text();
      public void map(LongWritable key, Text value, OutputCollector&lt;Text,DoubleWritable&gt; output,Reporter reporter)throws IOException{
          String data=value.toString();
          String arr[]=data.split("@");
          int i=2;
          double score=0;
          while(i&lt;arr.length){
              score+=Integer.parseInt(arr[i]);
              i++;
          }
          //Dividing The Score to give the average score in a particular course
          score=score/5;
          SID.set(arr[0]);
          output.collect(SID,new DoubleWritable(score));
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>  public static class Reduce extends MapReduceBase implements Reducer&lt;Text,DoubleWritable,Text,DoubleWritable&gt;{
      public void reduce(Text key,Iterator&lt;DoubleWritable&gt; values,OutputCollector&lt;Text,DoubleWritable&gt; output,Reporter reporter)throws IOException{
          double Total=0.0;
          while(values.hasNext()){
              Total+=values.next().get(); 
          }
          //Dividing By 5 to obtain the average score for a particular student
          output.collect(key,new DoubleWritable((Total/5)));              
      }
  }
  </code></pre>
  
  <p>Besides, In The Main Class, apart from defining other configurations, I have set the above <code>Reduce</code> class as both the <code>Reducer</code> as well as the <code>Combiner</code> class.</p>
  
  <p>However the output I obtained was as following</p>
  
  <pre><code> 01001  9.879999999999999
   01002  10.568
   01003  8.712
   01004  10.68
   01005  9.335999999999999
   ....
  </code></pre>
  
  <p>This shows that the total score of a student is being divided by 125 and not 25.
  However, on removing the statement <code>score=score/5</code> from the <code>Map</code> class,
  I got the correct result. Now from what I could understand (and am not very sure about that),it is because the <code>Reducer</code> and <code>Combiner</code> classes are the same in this case. Is that so?
  How is the <code>Combiner</code> class working in this case?</p>