list 是用来列出HBase中所有表的命令。下面给出了 list 命令的语法。
hbase(main):001:0 > list |
当输入这个命令,并在HBase提示符下执行,它会显示HBase中的所有表的列表,如下图所示。
hbase(main):001:0> list |
TABLE |
emp |
在这里,可以看到一个名为表emp。
按照下面给出的步骤来使用Java API从HBase获得表的列表。
在类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(); |
就可以得到使用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 |
User |
emp |