Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if table exists

Tags:

java

hadoop

hbase

What is the fastest way to check if Hbase table exists? Looking at this api :

http://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/client/HBaseAdmin.html Which of these is the fastest :

  1. tableExists
  2. isTableEnabled
  3. isTableAvailable
  4. listTables

With #4 you get list of all tables and iterate trough it and compare if one of those tables matches your table name.

Or there is another, more smart way ?

like image 678
ant Avatar asked Feb 03 '11 15:02

ant


3 Answers

Here is my sample code. (scala)

import org.apache.hadoop.hbase.HBaseConfiguration

var TableName = "sample"
val conf = HBaseConfiguration.create()
var hbaseAdmin = new HBaseAdmin(conf)
if (!hbaseAdmin.tableExists(TableName)) {
  println(TableName + " Does Not Exist")
}

Here, you just need to use "tableExists" to check whether this TableName exists.

like image 162
Haimei Avatar answered Sep 30 '22 13:09

Haimei


  HBaseAdmin hba = new HBaseAdmin(hbaseTemplate.getConfiguration());
    if (hba.tableExists(tableName) == false) {

        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamilyProfile);
        tableDescriptor.addFamily(columnDescriptor);

        hba.createTable(tableDescriptor);
    }
like image 42
najeeb Avatar answered Sep 30 '22 12:09

najeeb


Using HBaseAdmin.tableExists only takes about 500ms to check if the table exists. We only have two nodes in our cluster, so it might be dependent on the size of your cluster, but it doesn't seem unreasonably slow.

like image 30
shad Avatar answered Sep 30 '22 11:09

shad