我用的版本是0.96.2的,api和之前的版本有许多不同之处,更接近最新的1.0版本的api,不过官网给出的api很多没有方法的功能介绍,看起来实在是麻烦,也算是HbaseApi的不足之处吧。
客户端创建表需要用HbaseAdmin,而之前的HTable则只是操作表的增删改查,基于数据的,而不是表结构等管理功能。
/** * 客户端建表 */
public static void testClientCreateTable() {
try {
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("user_table"));
HColumnDescriptor columDesc = new HColumnDescriptor("info".getBytes());
desc.addFamily(columDesc);
admin.createTable(desc);
boolean isAvailable = admin.isTableAvailable("user_table".getBytes());
log.info(isAvailable);
} catch (Exception e) {
log.error(e);
}
}
HTableDescriptor描述一张表的基本信息。
/** * 获取表结构信息 */
public static void testTableStruct() {
try {
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor[] descs = admin.listTables();
for (HTableDescriptor hTableDescriptor : descs) {
log.info(hTableDescriptor);
}
HTableDescriptor desc = admin.getTableDescriptor("user_table".getBytes());
log.info(desc);
} catch (Exception e) {
log.error(e);
}
}
删除表,需要先把表禁用才能进行删除,否则无法删除。禁用后,通过启用恢复表的正常使用。
/** * 删除表 */
public static void testDeleteTable() {
try {
HBaseAdmin admin = new HBaseAdmin(config);
boolean enable = admin.isTableEnabled("counters");
log.info(enable);
// 禁用
admin.disableTable("counters");
boolean enableL = admin.isTableEnabled("counters");
log.info(enableL);
// 启用
admin.enableTable("counters");
boolean enableC = admin.isTableEnabled("counters");
log.info(enableC);
admin.disableTable("counters");
admin.deleteTable("counters");
boolean bool = admin.isTableAvailable("counters");
log.info(bool);
} catch (Exception e) {
log.info(e);
}
}
修改表时,需要先禁用表,然后执行修改操作,最后启用表,使得表恢复可用状态。
/** * 修改表结构 */
private static void testModifyTable() {
try {
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor desc = admin.getTableDescriptor("testtable".getBytes());
// 添加一个列族
HColumnDescriptor family = new HColumnDescriptor("col2");
desc.addFamily(family);
admin.disableTable("testtable");
admin.modifyTable("testtable", desc);
admin.enableTable("testtable");
HTableDescriptor desc2 = admin.getTableDescriptor("testtable".getBytes());
log.info(desc2);
} catch (Exception e) {
log.error(e);
}
}
以下代码,展示了集群状态的api,可以获取负载、节点名称、主节点信息、端口、压缩大小、版本、宕掉的节点数目以及相应的RegionServer的信息,以及Region的负载信息等等。
/** * 获取集群状态 */
private static void testClusterStatus() {
try {
HBaseAdmin admin = new HBaseAdmin(config);
ClusterStatus status = admin.getClusterStatus();
// 获取平均负载
log.info("AverageLoad:" + status.getAverageLoad());
// 备份主节点
log.info("BackupMasters:" + status.getBackupMasters());
// 备份主节点数量
log.info("BackupMastersSize:" + status.getBackupMastersSize());
log.info("BalancerOn:" + status.getBalancerOn());
log.info("ClusterId:" + status.getClusterId());
// 获取DeadServerList
log.info("DeadServerNames:" + status.getDeadServerNames());
// 获取DeadServer 数目
log.info("DeadServers:" + status.getDeadServers());
// Hbase 版本信息
log.info("HBaseVersion:" + status.getHBaseVersion());
// Hbase Regions数量
log.info("RegionsCount:" + status.getRegionsCount());
// 自上次的请求数目
log.info("RequestsCount:" + status.getRequestsCount());
// RegionServer的数量
log.info("ServersSize:" + status.getServersSize());
// 返回当前主节点的详细信息
log.info("ServersSize:" + status.getMaster());
log.info(">>>>>>>>>>>>>>>>>ServerName>>>>>>>>>>>>>>>>>>");
for (ServerName server : status.getServers()) {
log.info("ServerName:" + server.getServerName());
log.info("HostAndPort:" + server.getHostAndPort());
log.info("Hostname" + server.getHostname());
log.info("Port:" + server.getPort());
ServerLoad load = status.getLoad(server);
//
log.info("CurrentCompactedKVs:" + load.getCurrentCompactedKVs());
// 最大可用堆大小
log.info("MaxHeapMB:" + load.getMaxHeapMB());
// 获取Region的数量
log.info("NumberOfRegions():" + load.getNumberOfRegions());
// 端口
log.info("InfoServerPort:" + load.getInfoServerPort());
// Memstore大小
log.info("MemstoreSizeInMB:" + load.getMemstoreSizeInMB());
// 请求数目
log.info("NumberOfRequests:" + load.getNumberOfRequests());
// 读请求数目
log.info("ReadRequestsCount:" + load.getReadRequestsCount());
// 每秒请求数目
log.info("RequestsPerSecond:" + load.getRequestsPerSecond());
// root索引大学
log.info("RootIndexSizeKB:" + load.getRootIndexSizeKB());
// storefile索引大小
log.info("StorefileIndexSizeInMB:" + load.getStorefileIndexSizeInMB());
//
log.info("getStores:" + load.getStores());
// 未压缩store大小
log.info("StoreUncompressedSizeMB:" + load.getStoreUncompressedSizeMB());
//
log.info("TotalCompactingKVs:" + load.getTotalCompactingKVs());
// 请求总数
log.info("TotalNumberOfRequests:" + load.getTotalNumberOfRequests());
//
log.info("TotalStaticBloomSizeKB:" + load.getTotalStaticBloomSizeKB());
// 静态索引大小
log.info("TotalStaticIndexSizeKB:" + load.getTotalStaticIndexSizeKB());
// 已经使用的堆大小
log.info("UsedHeapMB:" + load.getUsedHeapMB());
// 写请求数目
log.info("WriteRequestsCount:" + load.getWriteRequestsCount());
log.info("<<<<RegionsLoad>>>>>");
Map<byte[], RegionLoad> map = load.getRegionsLoad();
Set<Entry<byte[], RegionLoad>> entry = map.entrySet();
for (Entry<byte[], RegionLoad> entry2 : entry) {
RegionLoad regionLoad = entry2.getValue();
// 当前Region完成的序列id
log.info("getCompleteSequenceId:" + regionLoad.getCompleteSequenceId());
//
log.info("getCurrentCompactedKVs:" + regionLoad.getCurrentCompactedKVs());
// Memstore大小
log.info("getMemStoreSizeMB:" + regionLoad.getMemStoreSizeMB());
// region的name
log.info("getName:" + regionLoad.getName());
//
log.info("getNameAsString:" + regionLoad.getNameAsString());
// 读请求数目
log.info("getReadRequestsCount:" + regionLoad.getReadRequestsCount());
// 请求数目
log.info("getRequestsCount:" + regionLoad.getRequestsCount());
// root-level 索引的当前总大小
log.info("getRootIndexSizeKB:" + regionLoad.getRootIndexSizeKB());
// storefile索引大小
log.info("getStorefileIndexSizeMB:" + regionLoad.getStorefileIndexSizeMB());
// storefile数目
log.info("getStorefiles:" + regionLoad.getStorefiles());
// storefile大小
log.info("getStorefileSizeMB:" + regionLoad.getStorefileSizeMB());
// store数量
log.info("getStores:" + regionLoad.getStores());
// 未压缩的storefile的大小
log.info("getStoreUncompressedSizeMB:" + regionLoad.getStoreUncompressedSizeMB());
// the total number of kvs in current compaction
log.info("getTotalCompactingKVs:" + regionLoad.getTotalCompactingKVs());
// bloom过滤器块的总大小
log.info("getTotalStaticBloomSizeKB:" + regionLoad.getTotalStaticBloomSizeKB());
// 索引块总大小
log.info("getTotalStaticIndexSizeKB:" + regionLoad.getTotalStaticIndexSizeKB());
// 写请求总数
log.info("getWriteRequestsCount:" + regionLoad.getWriteRequestsCount());
}
}
} catch (Exception e) {
log.error(e);
}
}
转载注明出处:http://blog.csdn.net/wangyang1354/article/details/53761635