背景:HBase默認建表時有一個region,這個region的rowkey是沒有邊界的,即沒有startkey和endkey,在數據寫入時,所有數據都會寫入這個默認的region,隨着數據量的不斷 增加,此region已經不能承受不斷增長的數據量,會進行split,分成2個region。在此過程中,會產生兩個問題:1.數據往一個region上寫,會有寫熱點問題。2.region split會消耗寶貴的集羣I/O資源。基於此我們可以控制在建表的時候,創建多個空region,並確定每個region的起始和終止rowky,這樣只要我們的rowkey設計能均勻的命中各個region,就不會存在寫熱點問題。自然split的機率也會大大降低。當然隨着數據量的不斷增長,該split的還是要進行split。像這樣預先創建hbase表分區的方式,稱之爲預分區,下面給出一種預分區的實現方式:
首先看沒有進行預分區的表,startkey和endkey爲空。
-10,10-20,20-30,30-40,40-50,50-60,60-70,70-80,80-90,90-
在使用HBase API建表的時候,需要產生splitkeys二維數組,這個數組存儲的rowkey的邊界值。下面是java 代碼實現:
String[] keys = new String[] { "10|", "20|", "30|", "40|", "50|",
"60|", "70|", "80|", "90|" };
byte[][] splitKeys = new byte[keys.length][];
TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序
for (int i = 0; i < keys.length; i++) {
rows.add(Bytes.toBytes(keys[i]));
}
Iterator<byte[]> rowKeyIter = rows.iterator();
int i=0;
while (rowKeyIter.hasNext()) {
byte[] tempRow = rowKeyIter.next();
rowKeyIter.remove();
splitKeys[i] = tempRow;
i++;
}
return splitKeys;
需要注意的是,在上面的代碼中用treeset對rowkey進行排序,必須要對rowkey排序,否則在調用admin.createTable(tableDescriptor,splitKeys)的時候會出錯。創建表的代碼如下:
* 創建預分區hbase表
* @param tableName 表名
* @param columnFamily 列簇
* @return
*/
@SuppressWarnings("resource")
public boolean createTableBySplitKeys(String tableName, List<String> columnFamily) {
try {
if (StringUtils.isBlank(tableName) || columnFamily == null
|| columnFamily.size() < 0) {
log.error("===Parameters tableName|columnFamily should not be null,Please check!===");
}
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
return true;
} else {
HTableDescriptor tableDescriptor = new HTableDescriptor(
TableName.valueOf(tableName));
for (String cf : columnFamily) {
tableDescriptor.addFamily(new HColumnDescriptor(cf));
}
byte[][] splitKeys = getSplitKeys();
admin.createTable(tableDescriptor,splitKeys);//指定splitkeys
log.info("===Create Table " + tableName
+ " Success!columnFamily:" + columnFamily.toString()
+ "===");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
log.error(e);
return false;
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
log.error(e);
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
log.error(e);
return false;
}
return true;
}
在hbase shell中輸入命令san 'hbase:meta'查看建表結果:
從上圖可看出10個region均勻的分佈在了3臺regionserver上(集羣就3臺機器regionserver),達到預期效果。還可以在hbase的web UI界面中更加直觀的查看建表的預分區信息。
再看看寫數據是否均勻的命中各個region,是否能夠做到對寫請求的負載均衡:public class TestHBasePartition {
public static void main(String[] args) throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
HTable table = new HTable(conf, "testhbase");
table.put(batchPut());
}
private static String getRandomNumber(){
String ranStr = Math.random()+"";
int pointIndex = ranStr.indexOf(".");
return ranStr.substring(pointIndex+1, pointIndex+3);
}
private static List<Put> batchPut(){
List<Put> list = new ArrayList<Put>();
for(int i=1;i<=10000;i++){
byte[] rowkey = Bytes.toBytes(getRandomNumber()+"-"+System.currentTimeMillis()+"-"+i);
Put put = new Put(rowkey);
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zs"+i));
list.add(put);
}
return list;
}
}
- public class TestHBasePartition {
- public static void main(String[] args) throws Exception{
- HBaseAdmin admin = new HBaseAdmin(conf);
- HTable table = new HTable(conf, "testhbase");
- table.put(batchPut());
- }
- private static String getRandomNumber(){
- String ranStr = Math.random()+"";
- int pointIndex = ranStr.indexOf(".");
- return ranStr.substring(pointIndex+1, pointIndex+3);
- }
- private static List<Put> batchPut(){
- List<Put> list = new ArrayList<Put>();
- for(int i=1;i<=10000;i++){
- byte[] rowkey = Bytes.toBytes(getRandomNumber()+"-"+System.currentTimeMillis()+"-"+i);
- Put put = new Put(rowkey);
- put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zs"+i));
- list.add(put);
- }
- return list;
- }
- }</span>
我寫了1萬條數據,從Write Request Count一欄可以查看寫請求是否均勻的分佈到3臺機器上,實測我的達到目標,完成。參考文章: