没有预分区缺点
- 首先是热点写,我们总是会往最大的start-key所在的region写东西,因为我们的rowkey总是会比之前的大,并且hbase的是按升序方式排序的。所以写操作总是被定位到无上界的那个region中。
- 其次,由于写热点,我们总是往最大start-key的region写记录,之前分裂出来的region不会再被写数据,有点被打进冷宫的赶脚,它们都处于半满状态,这样的分布也是不利的。
- 如果在写比较频率的场景下,数据增长快,split的次数也会增多,由于split是比较耗时耗资源的,所以我们并不希望这种事情经常发生。
看到这些缺点,我们知道,在集群的环境中,为了得到更好的并行性,我们希望有好的load blance,让每个节点提供的请求处理都是均等的。我们也希望,region不要经常split,因为split会使server有一段时间的停顿,如何能做到呢?——随机散列与预分区
随机散列与预分区
这里取前2个字符预先分区。/**
* @ Author: keguang
* @ Date: 2018/11/17 14:13
* @ version: v1.0.0
* @ description:
*/
public class RowKeyAction {
/**
* 生成user_label预分区的startkey, endkey
* @return
*/
public static byte[][] getSplitKeys() {
String end = "|";
String[] keys = new String[]{"{0", "{1", "{2", "{3", "{4", "{5",
"{6", "{7", "{8", "{9", "{A", "{B", "{C", "{D", "{E", "{F"
};
byte[][] splitKeys = new byte[keys.length][];
TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序
for (int i = 0; i < keys.length; i++) {
rows.add(Bytes.toBytes(keys[i] + end));
}
Iterator<byte[]> rowKeyIter = rows.iterator();
int i = 0;
while (rowKeyIter.hasNext()) {
byte[] tempRow = rowKeyIter.next();
rowKeyIter.remove();
splitKeys[i] = tempRow;
i++;
}
return splitKeys;
}
public static byte[][] getSplitKeys2() {
String end = "|";
String[] keys0 = new String[]{"{0", "{1", "{2", "{3", "{4", "{5",
"{6", "{7", "{8", "{9", "{A", "{B", "{C", "{D", "{E", "{F"
};
String[] keys00 = new String[]{"3", "7", "B", "F"};
String[] keys = new String[64];
List<String> list = new ArrayList<>();
for(int i = 0;i < keys0.length;i++){
for(int j = 0;j < keys00.length;j++){
list.add(keys0[i] + keys00[j]);
}
}
int cnt = 0;
for(String key: list){
keys[cnt] = key;
cnt = cnt + 1;
}
byte[][] splitKeys = new byte[keys.length][];
TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序
for (int i = 0; i < keys.length; i++) {
rows.add(Bytes.toBytes(keys[i] + end));
}
Iterator<byte[]> rowKeyIter = rows.iterator();
int i = 0;
while (rowKeyIter.hasNext()) {
byte[] tempRow = rowKeyIter.next();
rowKeyIter.remove();
splitKeys[i] = tempRow;
i++;
}
return splitKeys;
}
}
/**
* @version v1.0.0
* @Author:keguang
* @Date:2018/9/5 11:15
*/
public class Demo{
// 新建 hbase 表
@Test
public void test4(){
String tbName = "hm2:flash_people";
HbaseUtil.initConnection();
List<String> family = new ArrayList<>();
family.add("info");
// 生成rowkeys预分区
byte[][] splitKeys = RowKeyAction.getSplitKeys();
boolean result = HbaseUtil.createTableBySplitKeys(tbName, family, splitKeys);
if(result){
System.out.println(tbName + " 建表成功...");
}
}
}
|