title: In-mapper combining and when does cleanup routine of mapper executes?
body: <p>I'm trying simple bi-gram (word pair) count, I tried with simple "pair" approach, now I just modified to try "Stripes" approach, but in Cleanup routine of mapper, somehow my all keys are same word pair (as in last word pair!) and counts.</p>
  
  <p>e.g. text input is:</p>
  
  <blockquote>
    <p>My name is foo.  Foo is new to Hadoop.</p>
  </blockquote>
  
  <p>In mapper my hashmap looks like:</p>
  
  <pre><code>((my, name), 1), ((name, is), 1), ((is, foo), 2), ((is, new), 1), ((new, to), 1), ((to, hadoop), 1)
  </code></pre>
  
  <p>But in Cleanup routine, I tried to print same hashmap, it looks like</p>
  
  <pre><code>((to, hadoop), 1), ((to, hadoop), 1), ((to, hadoop), 2), ((to, hadoop), 1), ((to, hadoop), 1), ((to, hadoop), 1)
  </code></pre>
  
  <p>My code looks like:</p>
  
  <pre><code>Map Class:
  private HashMap&lt;TextPair, Integer&gt; h = new HashMap&lt;TextPair, Integer&gt;();;
  
  void map(...) :
      ...
  StringTokenizer itr = new StringTokenizer(value.toString());            
  left = itr.nextToken();
  while(itr.hasMoreTokens()) {
  right = itr.nextToken();
  
  if(left != null &amp;&amp; right!= null) {
              **//I have to create new TextPair(key object) each time!** 
      key.set(new Text(left.toLowerCase()), new Text(right.toLowerCase()));
      //If key is there, just do count + 1 else add key with value 1
      if(h.containsKey(key)) {
              int total = h.get(key) + 1;         
          h.put(key, total);
      } else {
          System.out.println("key: "+ key.toString()+ " =&gt; 1");                       
          h.put(key, 1);
      }
              //context.write(key, one);
      }
      left = right;
  }
      ....
  
  void cleanup(...):
     Iterator&lt;Entry&lt;TextPair, Integer&gt;&gt; itr = h.entrySet().iterator();
     while(itr.hasNext()) {
      Entry&lt;TextPair, Integer&gt; entry = itr.next();
      TextPair key = entry.getKey();
      int total = entry.getValue().intValue();
      System.out.println("--- MAP CLEANUP ---: key: "+ key.toString() + " =&gt; Total: "+ total);
  
      context.write(key, new IntWritable(total));
  }
  ...
  </code></pre>
  
  <p>Note: TextPair is my custom key class.
  Any suggestion?</p>
  
  <h2>EDIT 1:</h2>
  
  <p>does cleanup routine of Map, executed at last after all map tasks are done? And hash is kind of "global", something wrong with that or my iterator?</p>
  
  <h2>EDIT 2:</h2>
  
  <p>I have to create new TextPair Key object at each iteration in map() before hashing, that's what the issue was .. its solved, but wondering why so? I used hash with Python so many times, its good, no pain, why I need to create new object each time, I don't understand.</p>