hbase 预分区

没有预分区缺点

  • 首先是热点写,我们总是会往最大的start-key所在的region写东西,因为我们的rowkey总是会比之前的大,并且hbase的是按升序方式排序的。所以写操作总是被定位到无上界的那个region中。
  • 其次,由于写热点,我们总是往最大start-key的region写记录,之前分裂出来的region不会再被写数据,有点被打进冷宫的赶脚,它们都处于半满状态,这样的分布也是不利的。
  • 如果在写比较频率的场景下,数据增长快,split的次数也会增多,由于split是比较耗时耗资源的,所以我们并不希望这种事情经常发生。

看到这些缺点,我们知道,在集群的环境中,为了得到更好的并行性,我们希望有好的load blance,让每个节点提供的请求处理都是均等的。我们也希望,region不要经常split,因为split会使server有一段时间的停顿,如何能做到呢?——随机散列与预分区

随机散列与预分区


这里取前2个字符预先分区。

/**
 * @ Author: keguang
 * @ Date: 2018/11/17 14:13
 * @ version: v1.0.0
 * @ description:
 */
public class RowKeyAction {
    /**
     * 生成user_label预分区的startkey, endkey
     * @return
     */
    public static byte[][] getSplitKeys() {
        String end = "|";

        String[] keys = new String[]{"{0", "{1", "{2", "{3", "{4", "{5",
                "{6", "{7", "{8", "{9", "{A", "{B", "{C", "{D", "{E", "{F"
        };
        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] + end));
        }
        Iterator<byte[]> rowKeyIter = rows.iterator();
        int i = 0;
        while (rowKeyIter.hasNext()) {
            byte[] tempRow = rowKeyIter.next();
            rowKeyIter.remove();
            splitKeys[i] = tempRow;
            i++;
        }
        return splitKeys;
    }

    public static byte[][] getSplitKeys2() {
        String end = "|";

        String[] keys0 = new String[]{"{0", "{1", "{2", "{3", "{4", "{5",
                "{6", "{7", "{8", "{9", "{A", "{B", "{C", "{D", "{E", "{F"
        };
        String[] keys00 = new String[]{"3", "7", "B", "F"};
        String[] keys = new String[64];


        List<String> list = new ArrayList<>();
        for(int i = 0;i < keys0.length;i++){
            for(int j = 0;j < keys00.length;j++){
                list.add(keys0[i] + keys00[j]);
            }
        }

        int cnt = 0;
        for(String key: list){
            keys[cnt] = key;
            cnt = cnt + 1;
        }

        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] + end));
        }
        Iterator<byte[]> rowKeyIter = rows.iterator();
        int i = 0;
        while (rowKeyIter.hasNext()) {
            byte[] tempRow = rowKeyIter.next();
            rowKeyIter.remove();
            splitKeys[i] = tempRow;
            i++;
        }
        return splitKeys;
    }
}

/**
 * @version v1.0.0
 * @Author:keguang
 * @Date:2018/9/5 11:15
 */
public class Demo{
    // 新建 hbase 表
    @Test
    public void test4(){
        String tbName = "hm2:flash_people";
        HbaseUtil.initConnection();
        List<String> family = new ArrayList<>();
        family.add("info");
        // 生成rowkeys预分区
        byte[][] splitKeys = RowKeyAction.getSplitKeys();
        boolean result = HbaseUtil.createTableBySplitKeys(tbName, family, splitKeys);
        if(result){
            System.out.println(tbName + " 建表成功...");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据技术派

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值