0

Is there a programmatic way to check if a table exists in YugaByte's YCQL (Cassandra) api?

For example, in Postgres one can do something like:

How to check if a table exists in a given schema

SELECT EXISTS (
   SELECT 1
   FROM   information_schema.tables 
   WHERE  table_schema = 'schema_name'
   AND    table_name = 'table_name'
   );

Is there an equivalent in YCQL?"

2 Answers2

3

Like SELECT COUNT(*) FROM system_schema.tables WHERE keyspace_name = 'yourkeyspace' AND table_name = 'yourtable'; ? works at least for Cassandra. The count isn't necessary you can just see if result set has anything. If your doing it to see if you should create the table you can just run the create statement with an IF NOT EXISTS clause and it will be a noop if its already there.

Chris Lohfink
  • 16,150
  • 1
  • 29
  • 38
2

Yes, you can do the same for YugaByte DB's YCQL. Here's an example that shows how to check for the existence of a keyspace and that of a table via cqlsh.

Setup:

cqlsh> CREATE KEYSPACE IF NOT EXISTS ksp;

cqlsh> CREATE TABLE IF NOT EXISTS ksp.t(k int PRIMARY KEY, v int);

To check if a keyspace exists

cqlsh> select count(*) from system_schema.keyspaces 
       where keyspace_name = 'ksp';


 count
-------
     1

(1 rows)
cqlsh> select count(*) from system_schema.keyspaces 
       where keyspace_name = 'non-existent-ksp';

 count
-------
     0

(1 rows)

To check if a table exists

cqlsh> select count(*) from system_schema.tables 
       where keyspace_name = 'ksp' and table_name = 't';

 count
-------
     1

(1 rows)
cqlsh> select count(*) from system_schema.tables 
        where keyspace_name = 'ksp' and table_name = 'non-existent-t';

 count
-------
     0

(1 rows)