title: I am trying to add up all the numbers in a file which contains numbers separated by space and are contained in multiple lines using MapReduce
body: <p>My output is coming wrong. The Input File is:</p>
  
  <p>1 2 3 4<br/>
  5 4 3 2</p>
  
  <p>Output should be key: sum      value:    24</p>
  
  <p>Output produced by MapReduce: key: sum    value: 34</p>
  
  <p>I am using OpenJDK 7 in Ubuntu 14.04 to run the jar file whereas, the jar file was created in Eclipse Juna and the java version used was Oracle JDK 7 to compile it.
  NumberDriver.java</p>
  
  <p>package numbersum;</p>
  
  <pre><code>import java.io.*;
  //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;
  import org.apache.hadoop.util.GenericOptionsParser;
  public class NumberDriver {
  
      public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
          // TODO Auto-generated method stub
              Configuration conf=new Configuration();
              String[] otherArgs=new GenericOptionsParser(conf,args).getRemainingArgs();
              if(otherArgs.length!=2)
              {
                  System.err.println("Error");
                  System.exit(2);
              }
              Job job=new Job(conf, "number sum");
              job.setJarByClass(NumberDriver.class);
              job.setMapperClass(NumberMapper.class);
              job.setReducerClass(NumberReducer.class);
              job.setOutputKeyClass(Text.class);
              job.setOutputValueClass(IntWritable.class);
              FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
              FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
              System.exit(job.waitForCompletion(true)?0:1);
      }
  
  }
  </code></pre>
  
  <p>NumberMapper.java</p>
  
  <pre><code>package numbersum;
  import java.io.*;
  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.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;
  //import org.hsqldb.Tokenizer;
  
  public class NumberMapper extends Mapper &lt;LongWritable, Text, Text, IntWritable&gt; 
      {
          int sum;
          public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException
          {
              StringTokenizer itr=new StringTokenizer(value.toString());
              while(itr.hasMoreTokens())
              {
                  sum+=Integer.parseInt(itr.nextToken());
              }
              context.write(new Text("sum"),new IntWritable(sum));
          }
      }
  </code></pre>
  
  <p>NumberReducer.java</p>
  
  <pre><code>package numbersum;
  import java.io.*;
  //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;
  //import org.apache.hadoop.util.GenericOptionsParser;
  
  public class NumberReducer extends Reducer &lt;Text, IntWritable, Text, IntWritable&gt;
      {
          public void reduce(Text key,Iterable&lt;IntWritable&gt; values, Context context)throws IOException, InterruptedException
          {
              int sum=0;
              for(IntWritable value:values)
                  {
                      sum+=value.get();
                  }
              context.write(key,new IntWritable(sum));
          }
      }
  </code></pre>