Reputation: 2949
I don't want any data in any of the tables in keyspace. So I decided to drop the keyspace if exist and create it immediately. I am using the below code to achieve the same.
CassandraConnector(conf).withSessionDo { session =>
session.execute(s"DROP KEYSPACE if EXISTS $keyspace")
session.execute("""CREATE KEYSPACE if NOT EXISTS %s
WITH replication = {'class':'SimpleStrategy','replication_factor':'1'};""".format(keyspace)
) }
But it is failing to create a keyspace. From the logs I could only see a warning stating that
Received a DROPPED notification for table test.table_tracker, but this keyspace is unknown in our metadata.
I have also tried using python cassandra driver. But the results are same. I believe there is some race condition and the drop keyspace happens async(correct me if I am wrong).
How do I synchronously drop and create a keyspace ?
Upvotes: 3
Views: 1372
Reputation: 11638
If you simply want to drop all the data from a table(s), you should consider using TRUNCATE which drops all data from a table while retaining its schema, i.e.:
TRUNCATE test.table_tracker;
Performing schema changes should be done sparingly, so you should avoid it when possible. In addition there are a number of different issues in all versions of cassandra around dropping and recreating a keyspace quickly, so the truncate route is much more reliable.
One problem you may be running into is that maybe you are not waiting on schema agreement after executing each operation which can be problematic especially in the case when manipulating the same keyspace/table in subsequent operations. The java-driver (and thus the spark connector) will only wait up to a configurable amount of time between schema changes. See this guidance in the java-driver metadata doc for guidance on how to wait for agreement, i.e.:
if (cluster.getMetadata().checkSchemaAgreement()) {
// schema is in agreement
} else {
// schema is not in agreement
}
Upvotes: 9