@Override
  public int getPartition(ImmutableBytesWritable key, V2 value, int numPartitions) {
    byte[] region = null;
    // Only one region return 0
    if (this.startKeys.length == 1) {
      return 0;
    }
    try {
      // Not sure if this is cached after a split so we could have problems
      // here if a region splits while mapping
      region = locator.getRegionLocation(key.get()).getRegion().getStartKey();
    } catch (IOException e) {
      LOG.error(e.toString(), e);
    }
    for (int i = 0; i < this.startKeys.length; i++) {
      if (Bytes.compareTo(region, this.startKeys[i]) == 0) {
        if (i >= numPartitions) {
          // cover if we have less reduces then regions.
          return (Integer.toString(i).hashCode() & Integer.MAX_VALUE) % numPartitions;
        }
        return i;
      }
    }
    // if above fails to find start key that match we need to return something
    return 0;
  }
This is used to partition the output keys into groups of keys. Keys are grouped according to the
regions that currently exist so that each reducer fills a single region so load is distributed.
@param <K2>
@param <V2>
hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapred/HRegionPartitioner.java