HBase 列表
使用 HBase Shell 列出表
list 是用于列出 HBase 中所有表的命令。下面给出了 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 类的长度变量获取
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
以下应该是输出: