检查表是否存在

发布于 2024-10-15 21:24:29 字数 960 浏览 1 评论 0原文

检查 Hbase 表是否存在的最快方法是什么?查看此 API:

http://hbase.apache。 org/devapidocs/org/apache/hadoop/hbase/client/HBaseAdmin.html 其中哪一个最快:

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

使用#4,您可以获得所有表的列表,并对其进行迭代,并比较这些表之一是否与您的表名称匹配。

或者还有另一种更聪明的方法?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怂人 2024-10-22 21:24:29

这是我的示例代码。 (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")
}

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

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.

神也荒唐 2024-10-22 21:24:29
  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);
    }
  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);
    }
§对你不离不弃 2024-10-22 21:24:29

使用 HBaseAdmin.tableExists 只需大约 500ms 即可检查表是否存在。我们的集群中只有两个节点,因此它可能取决于集群的大小,但它看起来并不是不合理的慢。

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.

三生池水覆流年 2024-10-22 21:24:29

您可以尝试打开 HTable 到表,(我认为)如果表不存在,它会抛出异常/错误(尚未工作,因此无法进行快速测试)。

这并不是 100% 有效,只是一个突发的想法。 :)

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. :)

帅气尐潴 2024-10-22 21:24:29

每次启动应用程序时,我都必须检查表是否存在。我已经在配置类中使用 Spring Boot 做了这个

这是代码,希望它有所帮助。

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

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);
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文