Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Cassandra table exists

Tags:

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 executing SELECT 1 FROM table and checking for exception but maybe there is a better way?

like image 836
Datageek Avatar asked Apr 15 '13 13:04

Datageek


1 Answers

As of 1.1 you should be able to query the system keyspace, schema_columnfamilies column family. If you know which keyspace you want to check, this CQL should list all column families in a keyspace:

SELECT columnfamily_name FROM schema_columnfamilies WHERE keyspace_name='myKeyspaceName'; 

The report describing this functionality is here: https://issues.apache.org/jira/browse/CASSANDRA-2477

Although, they do note that some of the system column names have changed between 1.1 and 1.2. So you might have to mess around with it a little to get your desired results.

Edit 20160523 - Cassandra 3.x Update:

Note that for Cassandra 3.0 and up, you'll need to make a few adjustments to the above query:

SELECT table_name  FROM system_schema.tables WHERE keyspace_name='myKeyspaceName'; 
like image 95
Aaron Avatar answered Sep 24 '22 01:09

Aaron