Hbase 提高寫入效率之 預分區

來源:互聯網
上載者:User

背景: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台機器上,實測我的達到目標,完成。參考文章:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.