【问题标题】:Check if table exists检查表是否存在
【发布时间】:2025-11-25 03:30:01
【问题描述】:

检查 Hbase 表是否存在的最快方法是什么?看看这个 api:

http://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/client/HBaseAdmin.html 以下哪个最快:

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

使用#4,您可以获得所有表的列表并遍历它并比较其中一个表是否与您的表名匹配。

或者还有其他更聪明的方法?

【问题讨论】:

  • 你可以自己测试一下,不是吗?
  • @Matt Ball 我做到了,这需要几分钟以上..这就是我寻找最快/更快方法的原因。

标签: java hadoop hbase


【解决方案1】:

这是我的示例代码。 (斯卡拉)

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")
}

这里,你只需要使用“tableExists”来检查这个TableName是否存在。

【讨论】:

    【解决方案2】:
      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】:

      使用 HBaseAdmin.tableExists 只需要大约 500 毫秒来检查表是否存在。我们的集群中只有两个节点,因此它可能取决于您的集群的大小,但它似乎并没有太慢。

      【讨论】:

        【解决方案4】:

        您可以尝试向表打开一个HTable,并且(我认为)如果该表不存在,它将引发异常/错误(尚未工作,因此无法进行快速测试)。

        这不是 100% 可行的,只是一个不合时宜的想法。 :)

        【讨论】:

          【解决方案5】:

          每次启动我的应用程序时,我都必须检查表是否存在。我在一个配置类中做了这个,带有弹簧启动

          这是代码,希望对你有帮助。

          @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);
              }
          }
          }
          

          【讨论】: