Hbase-listing-table
提供:Dev Guides
HBase-リスト表
HBase Shellを使用してテーブルをリストする
listは、HBaseのすべてのテーブルをリストするために使用されるコマンドです。 以下にリストコマンドの構文を示します。
hbase(main):001:0 > list
このコマンドを入力してHBaseプロンプトで実行すると、以下に示すように、HBaseのすべてのテーブルのリストが表示されます。
hbase(main):001:0> list
TABLE
emp
ここでは、empという名前のテーブルを観察できます。
Java APIを使用したテーブルのリスト
以下の手順に従って、Java APIを使用してHBaseからテーブルのリストを取得します。
ステップ1
HBaseのすべてのテーブルのリストを取得するには、クラス HBaseAdmin に* listTables()というメソッドがあります。 このメソッドは、 *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
出力は次のようになります。
User
emp