HBase的预分区设计(很实用的一篇文章)

 

如果知道hbase数据表的key的分布情况,就可以在建表的时候对hbase进行region的预分区。这样做的好处是防止大数据量插入的热点问题,提高数据插入的效率。

背景: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创建分区表

在使用HBase API建表的时候,需要产生splitkeys二维数组,这个数组存储的rowkey的边界值。下面是java 代码实现:

  1. private byte[][] getSplitKeys() {

  2. String[] keys = new String[] { "10|", "20|", "30|", "40|", "50|",

  3. "60|", "70|", "80|", "90|" };

  4. byte[][] splitKeys = new byte[keys.length][];

  5. TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序

  6. for (int i = 0; i < keys.length; i++) {

  7. rows.add(Bytes.toBytes(keys[i]));

  8. }

  9. Iterator<byte[]> rowKeyIter = rows.iterator();

  10. int i=0;

  11. while (rowKeyIter.hasNext()) {

  12. byte[] tempRow = rowKeyIter.next();

  13. rowKeyIter.remove();

  14. splitKeys[i] = tempRow;

  15. i++;

  16. }

  17. return splitKeys;

  18. }

为什么后面会跟着一个"|",是因为在ASCII码中,"|"的值是124,大于所有的数字和字母等符号,当然也可以用“~”(ASCII-126)。分隔文件的第一行为第一个region的stopkey,每行依次类推,最后一行不仅是倒数第二个region的stopkey,同时也是最后一个region的startkey。也就是说分区文件中填的都是key取值范围的分隔点。
需要注意的是,在上面的代码中用treeset对rowkey进行排序,必须要对rowkey排序,否则在调用admin.createTable(tableDescriptor,splitKeys)的时候会出错。创建表的代码如下:

  1. /**

  2. * 创建预分区hbase表

  3. * @param tableName 表名

  4. * @param columnFamily 列簇

  5. * @return

  6. */

  7. @SuppressWarnings("resource")

  8. public boolean createTableBySplitKeys(String tableName, List<String> columnFamily) {

  9. try {

  10. if (StringUtils.isBlank(tableName) || columnFamily == null

  11. || columnFamily.size() < 0) {

  12. log.error("===Parameters tableName|columnFamily should not be null,Please check!===");

  13. }

  14. HBaseAdmin admin = new HBaseAdmin(conf);

  15. if (admin.tableExists(tableName)) {

  16. return true;

  17. } else {

  18. HTableDescriptor tableDescriptor = new HTableDescriptor(

  19. TableName.valueOf(tableName));

  20. for (String cf : columnFamily) {

  21. tableDescriptor.addFamily(new HColumnDescriptor(cf));

  22. }

  23. byte[][] splitKeys = getSplitKeys();

  24. admin.createTable(tableDescriptor,splitKeys);//指定splitkeys

  25. log.info("===Create Table " + tableName

  26. + " Success!columnFamily:" + columnFamily.toString()

  27. + "===");

  28. }

  29. } catch (MasterNotRunningException e) {

  30. // TODO Auto-generated catch block

  31. log.error(e);

  32. return false;

  33. } catch (ZooKeeperConnectionException e) {

  34. // TODO Auto-generated catch block

  35. log.error(e);

  36. return false;

  37. } catch (IOException e) {

  38. // TODO Auto-generated catch block

  39. log.error(e);

  40. return false;

  41. }

  42. return true;

  43. }


在hbase shell中输入命令san 'hbase:meta'查看建表结果:

从上图可看出10个region均匀的分布在了3台regionserver上(集群就3台机器regionserver),达到预期效果。还可以在hbase的web UI界面中更加直观的查看建表的预分区信息。

再看看写数据是否均匀的命中各个region,是否能够做到对写请求的负载均衡:

 
  1. public class TestHBasePartition {

  2. public static void main(String[] args) throws Exception{

  3. HBaseAdmin admin = new HBaseAdmin(conf);

  4. HTable table = new HTable(conf, "testhbase");

  5. table.put(batchPut());

  6. }

  7.  
  8. private static String getRandomNumber(){

  9. String ranStr = Math.random()+"";

  10. int pointIndex = ranStr.indexOf(".");

  11. return ranStr.substring(pointIndex+1, pointIndex+3);

  12. }

  13.  
  14. private static List<Put> batchPut(){

  15. List<Put> list = new ArrayList<Put>();

  16. for(int i=1;i<=10000;i++){

  17. byte[] rowkey = Bytes.toBytes(getRandomNumber()+"-"+System.currentTimeMillis()+"-"+i);

  18. Put put = new Put(rowkey);

  19. put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zs"+i));

  20. list.add(put);

  21. }

  22. return list;

  23. }

  24. }


写了1万条数据,从Write Request Count一栏可以查看写请求是否均匀的分布到3台机器上,实测我的达到目标,完成。

二、在HBase shell中创建分区表,指定分区文件

在hbase shell中直接输入create,会看到如下的提示:

 

 
  1. Examples:

  2.  
  3. Create a table with namespace=ns1 and table qualifier=t1

  4. hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}

  5.  
  6. Create a table with namespace=default and table qualifier=t1

  7. hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}

  8. hbase> # The above in shorthand would be the following:

  9. hbase> create 't1', 'f1', 'f2', 'f3'

  10. hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}

  11. hbase> create 't1', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}

  12.  
  13. Table configuration options can be put at the end.

  14. Examples:

  15.  
  16. hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']

  17. hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']

  18. hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'

  19. hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }

  20. hbase> # Optionally pre-split the table into NUMREGIONS, using

  21. hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)

  22. hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}

  23. hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}

  24. hbase> create 't1', {NAME => 'f1'}, {NAME => 'if1', LOCAL_INDEX=>'COMBINE_INDEX|INDEXED=f1:q1:8|rowKey:rowKey:10,UPDATE=true'}

可以通过指定SPLITS_FILE的值指定分区文件,如果分区信息比较少,也可以直接用SPLITS分区。我们可以通过如下命令建一个分区表,指定第一步中生成的分区文件:

create 'split_table_test', 'cf', {SPLITS_FILE => 'region_split_info.txt'}

假如我还想对hbase表做一个SNAPPY压缩,应该怎么写呢?

create 'split_table_test',{NAME =>'cf', COMPRESSION => 'SNAPPY'}, {SPLITS_FILE => 'region_split_info.txt'}

这里注意,一定要将分区的参数指定单独用一个大括号扩起来,因为分区是针对全表,而不是针对某一个column family。

下面,我们登陆一下master的web页面<Hmaster:60010>,查看一下hbase的表信息,找到刚刚新建的预分区表,进入查看region信息:

我们看到第一个region是没有startkey的,最后一个region是没有stopkey的。

 

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值