背景: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為空白。
要進行預分區,首先要明確rowkey的取值範圍或構成邏輯,以我的rowkey組成為例:兩位隨機數+時間戳記+客戶號,兩位隨機數的範圍從00-99,於是我劃分了10個region來儲存資料,每個region對應的rowkey範圍如下:
-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台機器上,實測我的達到目標,完成。參考文章: