1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapred;
20
21 import java.io.IOException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.client.HTable;
29 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.apache.hadoop.mapred.JobConf;
32 import org.apache.hadoop.mapred.Partitioner;
33
34
35
36
37
38
39
40
41
42
43 @Deprecated
44 @InterfaceAudience.Public
45 @InterfaceStability.Stable
46 public class HRegionPartitioner<K2,V2>
47 implements Partitioner<ImmutableBytesWritable, V2> {
48 private final Log LOG = LogFactory.getLog(TableInputFormat.class);
49 private HTable table;
50 private byte[][] startKeys;
51
52 public void configure(JobConf job) {
53 try {
54 this.table = new HTable(HBaseConfiguration.create(job),
55 job.get(TableOutputFormat.OUTPUT_TABLE));
56 } catch (IOException e) {
57 LOG.error(e);
58 }
59
60 try {
61 this.startKeys = this.table.getStartKeys();
62 } catch (IOException e) {
63 LOG.error(e);
64 }
65 }
66
67 public int getPartition(ImmutableBytesWritable key,
68 V2 value, int numPartitions) {
69 byte[] region = null;
70
71 if (this.startKeys.length == 1){
72 return 0;
73 }
74 try {
75
76
77 region = table.getRegionLocation(key.get()).getRegionInfo().getStartKey();
78 } catch (IOException e) {
79 LOG.error(e);
80 }
81 for (int i = 0; i < this.startKeys.length; i++){
82 if (Bytes.compareTo(region, this.startKeys[i]) == 0 ){
83 if (i >= numPartitions-1){
84
85 return (Integer.toString(i).hashCode()
86 & Integer.MAX_VALUE) % numPartitions;
87 }
88 return i;
89 }
90 }
91
92 return 0;
93 }
94 }