封装创建表方法,并在建表时预分区
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;
}