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