I need to check if certain keyspace exists in Cassandra database. I need to write smth like this:
if (keyspace KEYSPACE_NAME not exists) create keyspace KEYSPACE_NAME;
There's a command describe keyspace, but can I somehow retrieve information from it in cql script?
Go to data tab > there you will see all keyspcaces created by you and some system keyspaces. You can see all tables under individual keyspaces and also replicator factor for keyspace.
If you want to look at the replication factor of a given keyspace, simply execute SELECT * FROM system_schema. keyspaces; and it will print all replication information you need.
To select a keyspace in Cassandra and perform actions on it, use the keyword USE . The CQL shell switches to the name of the keyspace you specified. To change the current keyspace, use the same command with another name. Note: Whenever you create a table in Cassandra, you start by defining the keyspace.
A keyspace is an object that is used to hold column families, user defined types. A keyspace is like RDBMS database which contains column families, indexes, user defined types, data center awareness, strategy used in keyspace, replication factor, etc. In Cassandra, "Create Keyspace" command is used to create keyspace.
Just providing fresh information. As of CQL3 while creating a keyspace you can add if statement like this
CREATE KEYSPACE IF NOT EXISTS Test
WITH replication = {'class': 'SimpleStrategy',
'replication_factor' : 3}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With