Search code examples
hbase

In HBase is there a way for me to get the middle key of a region?


It looks like I can get the middle key like so:

        RegionServerServices rss = null;
        final List<Region> onlineRegions = rss.getOnlineRegions(tableName);

        for (Region region : onlineRegions) {
            final List<Store> stores = region.getStores();
            for (Store store : stores) {
                final long storeSize = store.getSize();
                final byte[] splitPoint = store.getSplitPoint();

            }
        }
    }
}

However, there is no way for me to get an HRegion or a Region or a RegionServerService on the client side.


Solution

  • Disclaimer: I am not sure how safe this approach is, since it may put a read-lock on the hfile. I am going to try to use it on snapshots so I'll be ok.

    Also you may wish to :

    • get the midpoint of the store for a particular column family and
    • get the midpoint of the largest store, that may give you a better approximation when your region has multiple stores

    excuse the formatting....

        try (Connection connection = 
                    ConnectionFactory.createConnection(hbaseConfig);
                    Admin admin = connection.getAdmin();) {
        final List<HRegionInfo> regionInfoList = admin.getTableRegions(tableName);
        HRegionInfo regionInfo = regionInfoList.get(0);
        final HRegion region = HRegion.openHRegion(hbaseConfig,
    
        FileSystem.get(hbaseConfig),
                                                       new Path("/hbase"),
                                                       regionInfo,
                                                       table.getTableDescriptor(),
                                                       null,
                                                       null,
                                                       null);
         List<Store> stores = region.getStores();
         stores.get(0).getSplitPoint();
        }