一、预分区
1 原因: 热点问题:数据持续读写一个region(或节点)上。 region自动分区会造成频繁IO。
2 设置分区 (前缀)范围
-10,10-20,20-30,30-40,40-50,50-60,60-70,70-80,80-90,90-
生成分区前缀:
private byte[][] getSplitKeys() {
String[] keys = new String[] { "00_", "10_", "20_", "30_", "40_", "50_", "60_", "70_", "80_", "90_" };
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]));
}
Iterator<byte[]> rowKeyIter = rows.iterator();
int i = 0;
while (rowKeyIter.hasNext()) {
byte[] tempRow = rowKeyIter.next();
rowKeyIter.remove();
splitKeys[i] = tempRow;
i++;
}
return splitKeys;
}
建表:
public boolean createTableBySplitKeys(String tableName, List<String> columnFamily) {
try {
if (StringUtils.isBlank(tableName) || columnFamily == null || columnFamily.size() < 0) {
log.error("tableName|columnFamily不为null");
}
Admin admin = connection.getAdmin();
TableName tableName1 = TableName.valueOf(tableName);
if (admin.tableExists(tableName1)) {
return true;
} else {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName1);
for (String cf : columnFamily) {
builder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build());
}
byte[][] splitKeys = getSplitKeys();
admin.createTable(builder.build(), splitKeys);// 指定splitkeys
log.info("表" + tableName + "创建成功,列族:" + columnFamily.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
造数据:
private String getRandomNumber() {
String ranStr = Math.random() + "";
int pointIndex = ranStr.indexOf(".");
return ranStr.substring(pointIndex + 1, pointIndex + 3);
}
private List<Put> batchPut() {
List<Put> list = new ArrayList<Put>();
for (int i = 1; i <= 10000; i++) {
byte[] rowkey = Bytes.toBytes(getRandomNumber() + "-" + System.currentTimeMillis() + "-" + i);
Put put = new Put(rowkey);
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zs" + i));
list.add(put);
}
return list;
}
查看web ui以验证:http://localhost:16010/table.jsp?name=test 代码地址:https://github.com/xing54321/hbase_demo
二、注意点
1 如何加盐 2 每个RegionServer上分区多少合适
参考:
https://blog.csdn.net/u013870094/article/details/79440312. https://www.cnblogs.com/duanxz/p/3154487.html
|