How do I get the number of HFiles of a Hbase table

2019-09-13 05:34发布

I have a HBase table. I executed HBase major compaction for the table. How do I get the number of HFiles dynamically for the Hbase table in Java?

标签: hbase
2条回答
Summer. ? 凉城
2楼-- · 2019-09-13 06:19

Found this code sample in Kylin - you can get the number of store files from a RegionLoad instance, i.e. int storeFiles = regionLoad.getStorefiles()

/** Constructor for unit testing */
HBaseRegionSizeCalculator(HTable table, HBaseAdmin hBaseAdmin) throws IOException {

try {
    if (!enabled(table.getConfiguration())) {
        logger.info("Region size calculation disabled.");
        return;
    }

    logger.info("Calculating region sizes for table \"" + new String(table.getTableName()) + "\".");

    // Get regions for table.
    Set<HRegionInfo> tableRegionInfos = table.getRegionLocations().keySet();
    Set<byte[]> tableRegions = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);

    for (HRegionInfo regionInfo : tableRegionInfos) {
        tableRegions.add(regionInfo.getRegionName());
    }

    ClusterStatus clusterStatus = hBaseAdmin.getClusterStatus();
    Collection<ServerName> servers = clusterStatus.getServers();
    final long megaByte = 1024L * 1024L;

    // Iterate all cluster regions, filter regions from our table and
    // compute their size.
    for (ServerName serverName : servers) {
        ServerLoad serverLoad = clusterStatus.getLoad(serverName);

        for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) {
            byte[] regionId = regionLoad.getName();

            if (tableRegions.contains(regionId)) {

                long regionSizeBytes = regionLoad.getStorefileSizeMB() * megaByte;
                sizeMap.put(regionId, regionSizeBytes);

                // logger.info("Region " + regionLoad.getNameAsString()
                // + " has size " + regionSizeBytes);
            }
        }
    }
} finally {
    hBaseAdmin.close();
}

}

查看更多
欢心
3楼-- · 2019-09-13 06:22

You don't mention which version of HBase you are using but you may be able to use Hannibal for that.

查看更多
登录 后发表回答