Check if table exists
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 :
- tableExists
- isTableEnabled
- isTableAvailable
- 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 s开发者_StackOverflow中文版mart way ?
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.
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);
}
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.
You could attempt to open an HTable to the table and (I think) it will throw an exception/error (not at work yet so can't do a quick test) if the table doesn't exist.
Not 100% this will work, just an off the top of the head idea. :)
I have to check if table exist every time i start my app. I have made this in a configuration class, with spring boot
Here is the code, hope it helps.
@Configuration
public class CustomHbaseConfiguration {
@Bean
public Connection hbaseConnection() throws IOException {
// Create connection
final org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
// Validate that Hbase is available
HBaseAdmin.available(configuration);
// return the hbaseConnection Bean
return ConnectionFactory.createConnection(configuration);
}
@PostConstruct
public void hbaseTableLogic() throws IOException {
// With the hbaseConnection bean, get the HbaseAdmin instance
Admin admin = hbaseConnection().getAdmin();
// The name of my table
TableName YOUR_TABLE_NAME_HERE = TableName.valueOf("PUT_YOUR_TABLE_NAME_HERE");
// Check if the table already exists ? else : create table and colum family
if (!admin.tableExists(YOUR_TABLE_NAME_HERE)) {
HTableDescriptor hTableDescriptor = new HTableDescriptor(YOUR_TABLE_NAME_HERE);
hTableDescriptor.addFamily(new HColumnDescriptor("PUT_YOUR_COLUM_FAMILY_HERE"));
admin.createTable(hTableDescriptor);
}
}
}
精彩评论