每一个region维护着startRow与endRowKey,如果加入的数据符合某个region维护的rowKey范围,则该数据交给这个region维护。那么依照这个原则,我们可以将数据索要投放的分区提前大致的规划好,以提高HBase性能。
1、方法一:手动设定预分区
hbase> create 'staff','info','partition1',SPLITS => ['1000','2000','3000','4000']
hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase> create 't1', {NAME =>'f1', TTL => 180}, SPLITS => ['10', '20', '30', '40']
hbase> create 't1', {NAME =>'f1', TTL => 180}, {NAME => 'f2', TTL => 240}, \
SPLITS => ['10', '20', '30', '40']
2、方法二:生成16进制序列预分区
hbase> create 'staff2','info','partition2',{NUMREGIONS => 15, \
SPLITALGO => 'HexStringSplit'}
生成15个分区
NUMREGIONS => 15
HexStringSplit
这是HBase的一个类,生成16进制序列预分区
3、方法三:按照文件中设置的规则预分区
创建splits.txt文件内容如下
[victor@hadoop102 hbase]$ vim splits.txt
aaaa
bbbb
cccc
dddd
执行
hbase> create 'staff3','partition3',SPLITS_FILE => 'splits.txt'
4、方法四:使用JavaAPI创建预分区
//自定义算法,产生一系列Hash散列值存储在二维数组中
byte[][] splitKeys = 某个散列值函数
//创建HBaseAdmin实例
HBaseAdmin hAdmin = new HBaseAdmin(HBaseConfiguration.create());
//创建HTableDescriptor实例
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
//通过HTableDescriptor实例和散列值二维数组创建带有预分区的HBase表
hAdmin.createTable(tableDesc, splitKeys);
import org.apache.hadoop.conf.Configuration;
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 create_table_sample2 {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.80,192.168.1.81,192.168.1.82");
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
TableName table_name = TableName.valueOf("TEST1");
if (admin.tableExists(table_name)) {
admin.disableTable(table_name);
admin.deleteTable(table_name);
}
HTableDescriptor desc = new HTableDescriptor(table_name);
HColumnDescriptor family1 =
new HColumnDescriptor(constants.COLUMN_FAMILY_DF.getBytes());
family1.setTimeToLive(3 * 60 * 60 * 24); //过期时间
family1.setMaxVersions(3); //版本数
desc.addFamily(family1);
byte[][] splitKeys = {
Bytes.toBytes("row01"),
Bytes.toBytes("row02"),
};
admin.createTable(desc, splitKeys);
admin.close();
connection.close();
}
}