title: Global variable's value doesn't change after for Loop
body: <p>I am developing a hadoop project. I want to find customers in a certain day and then write those with the max consumption in that day. In my reducer class, for some reason, the global variable <em>max</em> doesn't change it's value after a for loop.</p>
  
  <p><strong>EDIT</strong> I want to find the customers with max consumption in a certain day. I have managed to find the customers in the date I want, but I am facing a problem in my Reducer class. Here is the code:</p>
  
  <p><strong>EDIT #2</strong> I already know that the values(consumption) are Natural numbers. So in my output file I want to be only the customers, of a certain day, with max consumption.</p>
  
  <p><strong>EDIT #3</strong> My input file is consisted of many data. It has three columns; the customer's id, the timestamp (yyyy-mm-DD HH:mm:ss) and the consumption</p>
  
  <p><strong>Driver class</strong></p>
  
  <pre><code>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.lib.input.FileInputFormat;
  import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  
  public class alicanteDriver {
  
      public static void main(String[] args) throws Exception {
          long t_start = System.currentTimeMillis();
          long t_end;
  
          Configuration conf = new Configuration();
          Job job = Job.getInstance(conf, "Alicante");
          job.setJarByClass(alicanteDriver.class);
  
          job.setMapperClass(alicanteMapperC.class);        
  
          //job.setCombinerClass(alicanteCombiner.class);
  
          job.setPartitionerClass(alicantePartitioner.class);
  
          job.setNumReduceTasks(2);
  
          job.setReducerClass(alicanteReducerC.class);
  
          job.setMapOutputKeyClass(Text.class);
          job.setMapOutputValueClass(IntWritable.class);
  
          job.setOutputKeyClass(Text.class);
          job.setOutputValueClass(IntWritable.class);
          FileInputFormat.addInputPath(job, new Path("/alicante_1y.txt"));
          FileOutputFormat.setOutputPath(job, new Path("/alicante_output"));
          job.waitForCompletion(true);
          t_end = System.currentTimeMillis();
  
          System.out.println((t_end-t_start)/1000);
      }
   }
  </code></pre>
  
  <p><strong>Mapper class</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.StringTokenizer;
  import org.apache.hadoop.io.IntWritable;
  import org.apache.hadoop.io.LongWritable;
  import org.apache.hadoop.io.Text;
  import org.apache.hadoop.mapreduce.Mapper;
  
  public class alicanteMapperC extends
          Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {
  
      String Customer = new String();
      SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date t = new Date();
      IntWritable Consumption = new IntWritable();
      int counter = 0;
  
      // new vars
      int max = 0;
  
      @Override
      public void map(LongWritable key, Text value, Context context)
              throws IOException, InterruptedException {
  
          Date d2 = null;
          try {
              d2 = ft.parse("2013-07-01 01:00:00");
          } catch (ParseException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          if (counter &gt; 0) {
  
              String line = value.toString();
              StringTokenizer itr = new StringTokenizer(line, ",");
  
              while (itr.hasMoreTokens()) {
                  Customer = itr.nextToken();
                  try {
                      t = ft.parse(itr.nextToken());
                  } catch (ParseException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  Consumption.set(Integer.parseInt(itr.nextToken()));
  
                  //sort out as many values as possible
                  if(Consumption.get() &gt; max) {
                      max = Consumption.get();
                  }
  
                  //find customers in a certain date
                  if (t.compareTo(d2) == 0 &amp;&amp; Consumption.get() == max) {
                      context.write(new Text(Customer), Consumption);
                  }
              }
          }
          counter++;
      }
  }
  </code></pre>
  
  <p><strong>Reducer class</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import org.apache.hadoop.io.IntWritable;
  import org.apache.hadoop.io.Text;
  import org.apache.hadoop.mapreduce.Reducer;
  import com.google.common.collect.Iterables;
  
  public class alicanteReducerC extends
          Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {
  
      public void reduce(Text key, Iterable&lt;IntWritable&gt; values, Context context)
              throws IOException, InterruptedException {
  
          int max = 0; //this var
  
          // declaration of Lists
          List&lt;Text&gt; l1 = new ArrayList&lt;Text&gt;();
          List&lt;IntWritable&gt; l2 = new ArrayList&lt;IntWritable&gt;();
  
          for (IntWritable val : values) {
              if (val.get() &gt; max) {
                  max = val.get();
              }
              l1.add(key);
              l2.add(val);
          }
  
          for (int i = 0; i &lt; l1.size(); i++) {
              if (l2.get(i).get() == max) {
                  context.write(key, new IntWritable(max));
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Some values of the Input file</strong></p>
  
  <pre><code>C11FA586148,2013-07-01 01:00:00,3
  C11FA586152,2015-09-01 15:22:22,3
  C11FA586168,2015-02-01 15:22:22,1
  C11FA586258,2013-07-01 01:00:00,5
  C11FA586413,2013-07-01 01:00:00,5
  C11UA487446,2013-09-01 15:22:22,3
  C11UA487446,2013-07-01 01:00:00,3
  C11FA586148,2013-07-01 01:00:00,4
  </code></pre>
  
  <p><strong>Output should be</strong></p>
  
  <pre><code>C11FA586258 5
  C11FA586413 5
  </code></pre>
  
  <p>I have searched the forums for a couple of hours, and still can't find the issue. Any ideas?</p>