如何在Java中获取Hbase表的HFile数量?

时间:2016-10-24 07:20:13

标签: hbase

我有一个HBase表。我为表执行了HBase主要压缩。 如何在Java中动态获取Hbase表的HFile数量?

2 个答案:

答案 0 :(得分:1)

您没有提到您正在使用的HBase版本,但您可以使用Hannibal

答案 1 :(得分:1)

在Kylin中找到此代码示例 - 您可以从RegionLoad实例获取存储文件的数量,即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();
}

}