要查看HBase中的數據量,您可以使用HBase Shell或HBase的Java API。以下是使用HBase Shell和Java API的示例。
使用HBase Shell:
hbase(main):001:0> count 'table_name'
使用HBase的Java API:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseDataSize {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("table_name");
long dataSize = 0;
RegionLocator regionLocator = connection.getRegionLocator(tableName);
for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) {
ServerName serverName = regionLocation.getServerName();
byte[] regionName = regionLocation.getRegionInfo().getRegionName();
byte[] startRow = regionLocation.getRegionInfo().getStartKey();
byte[] endRow = regionLocation.getRegionInfo().getEndKey();
Scan scan = new Scan(startRow, endRow);
try (Table table = connection.getTable(tableName);
ResultScanner scanner = table.getScanner(scan)) {
for (Result result : scanner) {
dataSize += result.size();
}
}
}
System.out.println("Data size: " + dataSize + " bytes");
admin.close();
connection.close();
}
}
請確保將table_name
替換為要獲取數據量的實際表名。
以上是使用HBase Shell和Java API查看HBase數據量的方法。您可以根據需要選擇其中的一種方式。