hbase预分区

语法

create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'
# Optionally pre-split the table into NUMREGIONS, using
# SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}

  hbase shell实现

create 'fei', {NAME =>'f'}, SPLITS => [ '1', '2', '3','4','5','6','7','8','9']

效果

hbase预分区_第1张图片

scala代码实现

将hbase-site.xml放到resources目录

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.client.{ColumnFamilyDescriptorBuilder, ConnectionFactory, TableDescriptorBuilder}
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}

object HbaseCreate {
  def main(args: Array[String]): Unit = {
    //读取hbase-site.xml的方式创建hbaseConf
    val hbaseConf = HBaseConfiguration.create(new Configuration())
    val connection = ConnectionFactory.createConnection()
    val admin = connection.getAdmin();
    val tableName = TableName.valueOf("afei")
    //如果有表就删除
    if (admin.tableExists(tableName)) {
      admin.disableTable(tableName)
      admin.deleteTable(tableName)
    }
    val tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)

    val columnDescBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f"))
    columnDescBuilder.setMaxVersions(3)

    tableDescriptor.setColumnFamily(columnDescBuilder.build)

    //预分区
    /* val splitKeys = Array( Bytes.toBytes("1"), Bytes.toBytes("2"), Bytes.toBytes("3"), Bytes.toBytes("4"), Bytes.toBytes("5"),
       Bytes.toBytes("6"), Bytes.toBytes("7"), Bytes.toBytes("8"), Bytes.toBytes("9"))*/
    var splitKeys: List[Array[Byte]] = List()
    for (x <- 1 to 9) {
      if(x<10){
        splitKeys = splitKeys.+:(Bytes.toBytes(x.toString))
      }else{
        splitKeys = splitKeys.+:(Bytes.toBytes(x.toString))
      }

    }

    //创建表
    admin.createTable(tableDescriptor.build(), splitKeys.toArray)

    admin.close()
    connection.close()

    println("创建完成")
  }

}

 

你可能感兴趣的:(hbase预分区)