Updated on 2024-08-16 GMT+08:00

HBase Multi-Point Region Splitting

Function Description

You can perform multi-point splitting by using org.apache.hadoop.hbase.client.HBaseAdmin.

The splitting operation takes effect on empty regions only.

You can pre-partition a table when you create the table or directly split some regions.

In this example, the multi-point splitting is performed on an HBase table by using multiSplit. The table will be split into four regions: "A~D", "D~F", "F~H", and "H~Z".

Sample Code

The following code snippets are in the testMultiSplit method in the HBaseExample class of the com.huawei.bigdata.hbase.examples packet.

public void testMultiSplit() {
    LOG.info("Entering testMultiSplit.");

    Table table = null;
    Admin admin = null;
    try {
        admin = conn.getAdmin();

        // initilize a HTable object
        table = conn.getTable(tableName);
        Set<HRegionInfo> regionSet = new HashSet<HRegionInfo>();
        List<HRegionLocation> regionList = conn.getRegionLocator(tableName).getAllRegionLocations();
        for (HRegionLocation hrl : regionList) {
            regionSet.add(hrl.getRegionInfo());
        }
        byte[][] sk = new byte[4][];
        sk[0] = "A".getBytes();
        sk[1] = "D".getBytes();
        sk[2] = "F".getBytes();
        sk[3] = "H".getBytes();
        for (HRegionInfo regionInfo : regionSet) {
            ((HBaseAdmin) admin).multiSplit(regionInfo.getRegionName(), sk);
        }
        LOG.info("MultiSplit successfully.");
    } catch (Exception e) {
        LOG.error("MultiSplit failed ", e);
    } finally {
        if (table != null) {
            try {
                // Close table object
                table.close();
            } catch (IOException e) {
                LOG.error("Close table failed ", e);
            }
        }
        if (admin != null) {
            try {
                // Close the Admin object.
                admin.close();
            } catch (IOException e) {
                LOG.error("Close admin failed ", e);
            }
        }
    }
    LOG.info("Exiting testMultiSplit.");
}