CREATE KEYSPACE IF NOT EXISTS in CQL

Sanjiv picture Sanjiv · Mar 27, 2014 · Viewed 7.1k times · Source

I am using below CQL syntax to create keyspace only if does not exists :

CREATE KEYSPACE IF NOT EXISTS Test WITH replication 
                = {'class':'SimpleStrategy', 'replication_factor':1};

but it is giving error :

Exception in thread "main" com.datastax.driver.core.exceptions.SyntaxError: line 1:23 extraneous input 'EXISTS' expecting '='
at com.datastax.driver.core.exceptions.SyntaxError.copy(SyntaxError.java:35)
at com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:256)
at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:172)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:91)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:83)

please help me on this ? whats wrong with syntax ?

Answer

BrianC picture BrianC · May 8, 2014

That does appear to be the proper syntax (ref: docs). I've just tried it myself on Cassandra 2.0.7.

The "if not exists" option is part of CQL3 which first came with Cassandra 1.2. I would double check your Cassandra version to see if this option is available.