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