问题
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?
回答1:
You don't mention which version of HBase you are using but you may be able to use Hannibal for that.
回答2:
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();
}
}
来源:https://stackoverflow.com/questions/40212724/how-do-i-get-the-number-of-hfiles-of-a-hbase-table-in-java