HBase优化设计
1.1 表的设计
对表的Region进行预分区, 加快新建表写入或批量写
rowkey散列设计, 考虑业务范围查询
列族最好1个, 不超过2个
版本号一般为1(只保留最新), 数据也可以设置TTL
minor compact尽量错峰(合理设置触发条件, 定时任务合并), major compact必须错峰
1.1.1 Pre-Creating Regions(预分区)
- 默认情况下,在创建HBase表的时候会自动创建一个region分区,当导入数据的时候,所有的HBase客户端都向这一个region写数据,直到这个region足够大了才进行切分。
- 一种可以加快批量写入速度的方法是通过预先创建一些空的regions,这样当数据写入HBase时,会按照region分区情况,在集群内做数据的负载均衡。
- 一般一个节点管理 200 左右region, 每个region尽量不超过10G
//第一种实现方式是使用admin对象的切分策略
//定义key的起始值, 结束值, hbase初始分区数量
//由hbase根据上面3个值进行自动分区
byte[] startKey = ...; // your lowest key
byte[] endKey = ...; // your highest key
int numberOfRegions = ...; // # of regions to create
admin.createTable(table, startKey, endKey, numberOfRegions);
/*
//第二种实现方式是用户自定义切片
//byte[][] splits = ...; // create your own splits
byte[][] splits = new byte[][] { Bytes.toBytes("100"),
Bytes.toBytes("200"), Bytes.toBytes("400"),
Bytes.toBytes("500") };
//第一个region:“ to 100”
//第二个region:“100 to 200”
//第三个region:“200 to 400”
//第四个region:“400 to 500”
//第五个region:“500 to ”
admin.createTable(table, splits);
*/
/**
* 第二种预分区
* @param partition 最小10
* @return 分区[10-1000]
*/
private static byte[][] initRegion(int partition) {
byte[][] region = new byte[partition - 1][];
if (partition <= 10) {
// 默认最少10个预分区
partition = 10;
region = new byte[partition - 1][];
for (int i = 0; i < partition - 1; i++) {
region[i] = ("" + (i + 1)).getBytes();
}
} else if (partition <= 100){
double step = 100d