Check if table exists

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

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

With # 4, you get a list of all tables and iterate through it and compare if one of these tables matches your table name.

Or is there another, smarter way?

+5
source share
4 answers

HBaseAdmin.tableExists 500 , , . , , .

+1
  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);
    }
+3

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 if this TableName exists.

+3
source

You can try to open HTable in the table, and (I think) this will throw an exception / error (not at work but cannot do a quick test) if the table does not exist.

Not 100% it will work, just at the top of the head. :)

0
source

All Articles