HBase实践: HBase 2.x版本使用api建表和预分区

封装创建表方法,并在建表时预分区

def autoCreateTable(tableName: String, regionSize: Int): Unit = {
    val connection = getConnection
    val admin = connection.getAdmin
    val hTable = TableName.valueOf(tableName)
    if (admin.tableExists(hTable)) {
      admin.disableTable(hTable)
      admin.deleteTable(hTable)
    }
    val columnDescriptor = ColumnFamilyDescriptorBuilder
      //创建列簇
      .newBuilder(Bytes.toBytes("info"))
      //设置最大版本号
      .setMaxVersions(1)
      //设置最小版本号
      .setMinVersions(1)
      //设置块缓存
      .setBlockCacheEnabled(true)
      //设置缓存大小
      .setBlocksize(2097152)
      .build()
    // HBase 2.x版本建表API
    val tableDescriptor = TableDescriptorBuilder
      .newBuilder(TableName.valueOf(tableName))
      .setColumnFamily(columnDescriptor)
      .build()
    val split = splitRegionUtil.genSplitKeys(regionSize)
    admin.createTable(tableDescriptor, split)
    admin.close()
    connection.close()
  }

封装的预分区代码:

public  byte[][] genSplitKeys(int regions){
        //定义一个存放分区键的数组
        String[] keys = new String[regions];
        //目前推算,region个数不会超过2位数,所以region分区键格式化为两位数字所代表的字符串
        DecimalFormat df = new DecimalFormat("00");
        for(int i = 0; i < regions; i ++){
            keys[i] = df.format(i)+"|";
        }

        byte[][] splitKeys = new byte[regions][];
        //生成byte[][]类型的分区键的时候,一定要保证分区键是有序的
        TreeSet<byte[]> treeSet = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
        for(int i = 0; i < regions; i++){
            treeSet.add(Bytes.toBytes(keys[i]));
        }

        Iterator<byte[]> splitKeysIterator = treeSet.iterator();
        int index = 0;
        while(splitKeysIterator.hasNext()){
            byte[] b = splitKeysIterator.next();
            splitKeys[index ++] = b;
        }
        return splitKeys;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XuTengRui

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

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

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

打赏作者

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

抵扣说明:

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

余额充值