当前位置: 移动技术网 > IT编程>数据库>其他数据库 > hbase 预分区与自动分区

hbase 预分区与自动分区

2019年03月29日  | 移动技术网IT编程  | 我要评论

我们知道,hbase在创建表的时候,会自动为表分配一个region
当一个
region过大达到默认的阈值时(默认10gb大小),hbase中该region将会进行split,分裂为2个region,以此类推。
表在进行split的时候,会耗费大量的资源,频繁的分区对hbase的性能有巨大的影响。
所以,hbase提供了预分区功能,即用户可以在创建表的时候对表按照一定的规则分区。

假设我们初始给它10个region,那么导入大量数据的时候,就会均衡到10个里面,显然比1个region要好很多。
可是我们应该创建多少个region呢?显然没有具体答案,要结合业务,根据表的rowkey进行设计。

一.强制拆分
预分区方法:
1.hbase shell 预分区
建立分区前,要先了解表的rowkey格式,rowkey为:两位随机数+时间戳+客户id
两位随机数的范围从00-99,划分范围:小于10,10-20,20-30,30-40,40-50,50-60,60-70,70-80,90+
hbase(main):001:0> create 'log1', 'cf1', splits => ['10','20','30','40','50','60','70','80','90']

启动webui
vi hbase-site.xml
添加
<property>
<name>hbase.master.info.port</name>
<value>60010</value>
</property>

浏览器中:
http://www.lhsxpumps.com/_h201:60010

通过配置文件加载
[hadoop@h201 ~]$ cat rs.txt
10
20
30
40
50
60
70
80
90
hbase(main):003:0> create 'log2', 'cf1', splits_file =>'/home/hadoop/rs.txt'

2.hbase api 预分区

import java.io.ioexception;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.hbase.client.hbaseadmin;
import org.apache.hadoop.hbase.hbaseconfiguration;
import org.apache.hadoop.hbase.hcolumndescriptor;
import org.apache.hadoop.hbase.htabledescriptor;
import org.apache.hadoop.hbase.tablename;
import org.apache.hadoop.hbase.client.admin;
import org.apache.hadoop.hbase.client.connection;
import org.apache.hadoop.hbase.client.connectionfactory;
import org.apache.hadoop.hbase.util.bytes;

public class cp {
    public static void main(string[] args) {
       hbaseconfiguration config = new hbaseconfiguration();
       config.set("hbase.zookeeper.quorum", "h201,h202,h203");
       string tablename = new string("ctest1");
   try{
      hbaseadmin admin = new hbaseadmin(config);
        if (admin.tableexists(tablename)) {
            admin.disabletable(tablename);
            admin.deletetable(tablename);
        }

        htabledescriptor tabledesc = new htabledescriptor(tablename);
        tabledesc.addfamily(new hcolumndescriptor("cf1"));
       
        byte[][] splitkeys = {
            bytes.tobytes("10"),
            bytes.tobytes("20"),
            bytes.tobytes("30")
        };

        admin.createtable(tabledesc, splitkeys);
        admin.close();
      }catch(ioexception e) {
            e.printstacktrace();
        }
    }
}

验证:
webui查看
ctest1有4个 预分区

====================================================

二.自动拆分(auto splitting)
1.
0.94 版本之前采用的是 constantsizeregionsplitpolicy 策略。
这个策略非常简单,从名字上就可以看出这个策 略就是按照固定大小来拆分region。它唯一用到的参数是: hbase.hregion.max.filesize, 默认值是 10g, 也就是当 region 的大小达到 10g 的时候, 会自动拆分成两个 region.

2.
0.94 版本之后,有了 increasingtoupperboundregionsplitpolicy 策略。并且默认使用的这种策略。这种策略从名字上就可以看出是限制不断增长的文件尺寸的策略。
这种策略使用的最大store file size依据 min(r^2 * “hbase.hregion.memstore.flush.size”, “hbase.hregion.max.filesize”),r代表同一台region server节点上的region的个数。比如,在默认memstore flush size为128mb且默认的max store size为10g时。(r为region的个数)
第一次拆分大小为:min(10g,1*1*128m)=128m
第二次拆分大小为:min(10g,3*3*128m)=1152m
第三次拆分大小为:min(10g,5*5*128m)=3200m
第四次拆分大小为:min(10g,7*7*128m)=6272m
第五次拆分大小为:min(10g,9*9*128m)=10g
第五次拆分大小为:min(10g,11*11*128m)=10g

可以看到,只有在第四次之后的拆分大小才为10g

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网