编程执行HBase管理任务
同样,我们可以使用HBase Java API编程执行HBase的管理类任务,如创建表、删除表、启用/禁用表等。
下面是执行HBase管理类任务的示例代码。
import java.io.IOException; import java.util.Collection; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.ClusterStatus; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class HBaseAdminUtil { private static Configuration configuration; // 管理HBase的配置信息 private static Connection connection; // 管理HBase连接 private static Admin admin; // 管理HBase数据库的表信息 // 连接HBase数据库 static { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://master:8020/hbase"); // configuration.set("hbase.zookeeper.quorum","hadoop,slave1,slave2"); try { System.out.println("正在和数据库建立连接..."); connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); System.out.println("连接已建立"); } catch (IOException e) { e.printStackTrace(); } } // 向表中添加列族 public void addColumnToTable(String tableName, String family) throws IOException { admin.addColumn(TableName.valueOf(tableName), new HColumnDescriptor(family)); System.out.println("增加列族: " + family + "到表" + tableName); } // 从表中删除列族 public void delColumnFromTable(String tableName, String family) throws IOException { admin.deleteColumn(TableName.valueOf(tableName), Bytes.toBytes(family)); System.out.println("删除列族: " + family + "从表" + tableName); } // 创建HBase表 public void createTableInHbase(String tableName, String family) throws IOException { HTableDescriptor tabledescriptor = new HTableDescriptor(TableName.valueOf(tableName)); tabledescriptor.addFamily(new HColumnDescriptor(family)); admin.createTable(tabledescriptor); } // 执行主合并 public void performMajorCompact(String tableName) throws IOException { TableName tbName = TableName.valueOf(tableName); Table table = connection.getTable(tbName); try { admin.majorCompact(tbName); System.out.println("Compaction done!"); } catch (Exception e) { System.out.println(e); } } // 执行minor-compact public void perfomrMinorcompact(String tableName) throws IOException, InterruptedException { admin.compact(TableName.valueOf(tableName)); } // 删除指定的表 public void deletetableFromHBase(String tableName) throws IOException { admin.deleteTable(TableName.valueOf(tableName)); } // 禁用指定的表 public void disableHBaseTable(String tableName) throws IOException { admin.disableTable(TableName.valueOf(tableName)); } // 启用指定的表 public void enableHBaseTable(String tableName) throws IOException { admin.enableTable(TableName.valueOf(tableName)); } // 刷新表 public void flushTable(String tableName) throws IOException { admin.flush(TableName.valueOf(tableName)); } // 获取集群状态 public ClusterStatus getHBaseclusterstatus() throws IOException { return admin.getClusterStatus(); } // 打印集群状态详细信息 public void printClusterDetails() throws IOException { ClusterStatus clusterStatus = getHBaseclusterstatus(); Collectionserverinfo = clusterStatus.getServers(); for (ServerName s : serverinfo) { System.out.println("Server name " + s.getServerName()); System.out.println("Host name " + s.getHostname()); System.out.println("Host name : Port " + s.getHostAndPort()); System.out.println("Info port" + s.getPort()); System.out.println("Server load " + s.getStartcode()); System.out.println(); } String version = clusterStatus.getHBaseVersion(); System.out.println("Version " + version); int regioncounts = clusterStatus.getRegionsCount(); System.out.println("Region Counts :" + regioncounts); int servers = clusterStatus.getServersSize(); System.out.println("Servers :" + servers); double averageload = clusterStatus.getAverageLoad(); System.out.println("Average load: " + averageload); int deadservers = clusterStatus.getDeadServers(); System.out.println("Deadservers : " + deadservers); Collection Servernames = clusterStatus.getDeadServerNames(); for (ServerName s : Servernames) { System.out.println("Dead Servernames " + s.getServerName()); } } // 判断表是否可用 public void isHBaseTableAvailable(String tableName) throws IOException { boolean result = admin.isTableAvailable(TableName.valueOf(tableName)); System.out.println("Table " + tableName + " available ?" + result); } // 判断表是否启用 public void isHBaseTableEnabled(String tableName) throws IOException { boolean result = admin.isTableEnabled(TableName.valueOf(tableName)); System.out.println("Table " + tableName + " enabled ?" + result); } // 判断表是否禁用 public void isHBaseTableDisabled(String tableName) throws IOException { boolean result = admin.isTableDisabled(TableName.valueOf(tableName)); System.out.println("Table " + tableName + " disabled ?" + result); } // 判断表是否存在 public void checkIfTableExists(String tableName) throws IOException { boolean result = admin.tableExists(TableName.valueOf(tableName)); System.out.println("Table " + tableName + " exists ?" + result); } // 关闭集群 public void shutdownCluster() throws IOException { System.out.println("Shutting down.."); admin.shutdown(); } // 列出所有的HBase表 public void listAllTablesInHBase() throws IOException { admin.listTables(); } // 修改列族 public void modifyTableColumn(String tableName, String family) throws IOException { admin.modifyColumn(TableName.valueOf(tableName), new HColumnDescriptor(family)); } // 更改表名 public void modifyHBaseTable(String tableName, String hbaseNewTableName) throws IOException { admin.modifyTable(TableName.valueOf(tableName), new HTableDescriptor(TableName.valueOf(hbaseNewTableName))); } // 分割表 public void splitHBaseTable(String tableName) throws IOException, InterruptedException { admin.split(TableName.valueOf(tableName)); } // 检查HMaster是否在运行 public void checkIfMasterRunning() throws MasterNotRunningException, ZooKeeperConnectionException { System.out.println("Master running ? " + admin.isAborted()); } }