在hbase中,表中可能会存在很多小的region,如果不需要那么多小的region,可以将他们就行合并。
代码的逻辑是将相邻的两个小region进行合并,需要注意以下几点
1、当个region不宜过大,如果两个合并后超过当个region 的最大值,比如7G+7G=14G,超过region配置的最大值10G,这样的话哪怕合并了,也会重新分裂。
2、必须要相邻的两个region,才可以执行命令,不明白原理,不可随意修改代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
public class HbaseMerge {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.zookeeper.quorum", "h1,h2,h3");
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("hbase:meta"));
//merge表名
String tableName = "库名:表名";
Admin admin = connection.getAdmin();
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(tableName));
scan.withStopRow(Bytes.toBytes(tableName + "_"));
ResultScanner scanner = table.getScanner(scan);
ArrayList<String> regions = new ArrayList<>();
for (Result result : scanner) {
String[] split = result.toString().split("\\./");
if (split.length > 1) {
String s = split[0];
String[] split1 = s.split("\\.");
String region = split1[split1.length - 1];
regions.add(region);
}
}
System.out.println("共有region: " + regions.size() + "个-----------------");
for (int i = 0; i < regions.size() - 1; i += 2) {
String region1 = regions.get(i);
String region2 = regions.get(i + 1);
try {
admin.mergeRegions(region1.getBytes(), region2.getBytes(), false);
System.out.println(region1 + " - " + region2 + " merge is ok");
} catch (Exception e) {
System.out.println(region1 + " - " + region2 + " merge is fail fail fail");
}
}
//admin.majorCompact(TableName.valueOf(tableName)); 执行major,看是否需要开启
admin.close();
connection.close();
}
}