天天看點

HBASE預先配置設定regions的實作

http://blog.csdn.net/yiboo/article/details/7284111

前面寫了HBASE通過預先建立regions,來平衡資料的負載,其中用到了hbase官方的example

但是沒有人告訴你怎麼用

自己試了試用法

主要的就是如何配置設定rowkey start end之間的關系,因為我的資料的key是md5值,是以我使用了md5的兩段分為300份

public static void main(String[] agrs) {

HBaseAdmin admin;

try {

admin = new HBaseAdmin(conf);

HTableDescriptor tableDesc = new HTableDescriptor("test");

byte[][] splits =getHexSplits("100000000000000000", "ffffffffffffffffffff",

300);

createTable( admin, tableDesc,splits);

} catch (MasterNotRunningException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ZooKeeperConnectionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static boolean createTable(HBaseAdmin admin, HTableDescriptor table,

byte[][] splits) throws IOException {

try {

admin.createTable(table, splits);

return true;

} catch (TableExistsException e) {

LOG.info("table " + table.getNameAsString() + " already exists");

// the table already exists...

return false;

}

}

public static byte[][] getHexSplits(String startKey, String endKey,

int numRegions) {

byte[][] splits = new byte[numRegions - 1][];

BigInteger lowestKey = new BigInteger(startKey, 16);

BigInteger highestKey = new BigInteger(endKey, 16);

BigInteger range = highestKey.subtract(lowestKey);

BigInteger regionIncrement = range.divide(BigInteger

.valueOf(numRegions));

lowestKey = lowestKey.add(regionIncrement);

for (int i = 0; i < numRegions - 1; i++) {

BigInteger key = lowestKey.add(regionIncrement.multiply(BigInteger

.valueOf(i)));

byte[] b = String.format("%016x", key).getBytes();

splits[i] = b;

}

return splits;

}

運作完後大家可以看到http://host:60010/master-status中已經有了numberOfOnlineRegions=101,如果建立2個表則則是201

Region Servers

ServerName Start time Load
dn1,60020,1329986704703 Thu Feb 23 16:45:04 CST 2012 requestsPerSecond=0, numberOfOnlineRegions=201, usedHeapMB=62, maxHeapMB=995
dn2,60020,1329986707893 Thu Feb 23 16:45:07 CST 2012 requestsPerSecond=0, numberOfOnlineRegions=201, usedHeapMB=51, maxHeapMB=995
namenode,60020,1329986696234 Thu Feb 23 16:44:56 CST 2012 requestsPerSecond=0, numberOfOnlineRegions=202, usedHeapMB=40, maxHeapMB=995
Total: servers: 3 requestsPerSecond=0, numberOfOnlineRegions=604