How to check if a Cassandra table exists

后端 未结 3 1429
南旧
南旧 2021-02-07 01:35

Is there an easy way to check if table (column family) is defined in Cassandra using CQL (or API perhaps, using com.datastax.driver)?

Right now I am leaning towards exec

3条回答
  •  猫巷女王i
    2021-02-07 02:16

    The Java driver (since you mentioned it in your question) also maintains a local representation of the schema.

    Driver 3.x and below:

    KeyspaceMetadata ks = cluster.getMetadata().getKeyspace("myKeyspace");
    TableMetadata table = ks.getTable("myTable");
    boolean tableExists = (table != null);
    

    Driver 4.x and above:

    Metadata metadata = session.getMetadata();
    boolean tableExists =
      metadata.getKeyspace("myKeyspace")
        .flatMap(ks -> ks.getTable("myTable"))
        .isPresent();
    

提交回复
热议问题