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;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.hbase.regionserver.HRegion;
25 import org.apache.hadoop.hbase.util.Bytes;
26
27
28
29
30 public class MultiRegionTable extends HBaseClusterTestCase {
31 protected static final byte [][] KEYS = {
32 HConstants.EMPTY_BYTE_ARRAY,
33 Bytes.toBytes("bbb"),
34 Bytes.toBytes("ccc"),
35 Bytes.toBytes("ddd"),
36 Bytes.toBytes("eee"),
37 Bytes.toBytes("fff"),
38 Bytes.toBytes("ggg"),
39 Bytes.toBytes("hhh"),
40 Bytes.toBytes("iii"),
41 Bytes.toBytes("jjj"),
42 Bytes.toBytes("kkk"),
43 Bytes.toBytes("lll"),
44 Bytes.toBytes("mmm"),
45 Bytes.toBytes("nnn"),
46 Bytes.toBytes("ooo"),
47 Bytes.toBytes("ppp"),
48 Bytes.toBytes("qqq"),
49 Bytes.toBytes("rrr"),
50 Bytes.toBytes("sss"),
51 Bytes.toBytes("ttt"),
52 Bytes.toBytes("uuu"),
53 Bytes.toBytes("vvv"),
54 Bytes.toBytes("www"),
55 Bytes.toBytes("xxx"),
56 Bytes.toBytes("yyy")
57 };
58
59 protected final byte [] columnFamily;
60 protected HTableDescriptor desc;
61
62
63
64
65 public MultiRegionTable(final String familyName) {
66 this(1, familyName);
67 }
68
69 public MultiRegionTable(int nServers, final String familyName) {
70 super(nServers);
71
72 this.columnFamily = Bytes.toBytes(familyName);
73
74 System.setProperty("hadoop.log.dir", conf.get("hadoop.log.dir"));
75 conf.set("mapred.output.dir", conf.get("hadoop.tmp.dir"));
76 }
77
78
79
80
81 @Override
82 protected void preHBaseClusterSetup() throws Exception {
83 try {
84
85 HRegion[] regions = new HRegion[KEYS.length];
86 for (int i = 0; i < regions.length; i++) {
87 int j = (i + 1) % regions.length;
88 regions[i] = createARegion(KEYS[i], KEYS[j]);
89 }
90
91
92
93
94 createRootAndMetaRegions();
95
96 for(int i = 0; i < regions.length; i++) {
97 HRegion.addRegionToMETA(meta, regions[i]);
98 }
99
100 closeRootAndMeta();
101 } catch (Exception e) {
102 shutdownDfs(dfsCluster);
103 throw e;
104 }
105 }
106
107 private HRegion createARegion(byte [] startKey, byte [] endKey) throws IOException {
108 HRegion region = createNewHRegion(desc, startKey, endKey);
109 addContent(region, this.columnFamily);
110 closeRegionAndDeleteLog(region);
111 return region;
112 }
113
114 private void closeRegionAndDeleteLog(HRegion region) throws IOException {
115 region.close();
116 region.getLog().closeAndDelete();
117 }
118 }