HBase - 列出表格



使用 HBase Shell 列出表格

list 是用於列出 HBase 中所有表格的命令。以下是 list 命令的語法。

hbase(main):001:0 > list

當您鍵入此命令並在 HBase 提示符中執行時,將會顯示 HBase 中所有表格的列表,如下所示。

hbase(main):001:0> list
TABLE
emp

在此處,您可以觀察到名為 emp 的表格。

使用 Java API 列出表格

按照以下給定的步驟,可以使用 Java API 從 HBase 獲取表格列表。

步驟 1

您在 HBaseAdmin 類中有一個名為 listTables() 的方法,以獲取 HBase 中所有表格的列表。此方法返回一個 HTableDescriptor 物件陣列。

//creating a configuration object
Configuration conf = HBaseConfiguration.create();

//Creating HBaseAdmin object
HBaseAdmin admin = new HBaseAdmin(conf);

//Getting all the list of tables using HBaseAdmin object
HTableDescriptor[] tableDescriptor = admin.listTables();

步驟 2

您可以使用 HTableDescriptor 類的 length 變數來獲取 HTableDescriptor[] 陣列的長度。使用 getNameAsString() 方法從該物件獲取表格名稱。使用這些方法執行“for”迴圈,並獲取 HBase 中的表格列表。

以下是使用 Java API 列出 HBase 中所有表格的程式。

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class ListTables {

   public static void main(String args[])throws MasterNotRunningException, IOException{

      // Instantiating a configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Getting all the list of tables using HBaseAdmin object
      HTableDescriptor[] tableDescriptor = admin.listTables();

      // printing all the table names.
      for (int i=0; i<tableDescriptor.length;i++ ){
         System.out.println(tableDescriptor[i].getNameAsString());
      }
   
   }
}

按如下所示編譯和執行上述程式。

$javac ListTables.java
$java ListTables

應遵循以下輸出

User
emp
廣告
© . All rights reserved.