Updated July 6, 2023
What is Keyspace in Cassandra?
Keyspace in Cassandra is an object which is a collection of one or more column families(tables) schema. A keyspace is the outermost container of data. In Cassandra, the keyspace is somewhat analogous to a database in RDBS. A keyspace has a set of attributes that define the wide behavior like replication factor, datacenter awareness, the strategy used to hold the replicas, etc. We can have more than one keyspace in a cluster. Having more than one keyspace per application is also possible, but avoiding an unstructured data model is not recommended.
How to Create a Keyspace in Cassandra?
Go to your machine’s Cassandra installation directory, start cqlsh, and run the “CREATE KEYSPACE” command with properties.
Syntax:
CREATE {KEYSPACE | SCHEMA } [ IF NOT EXISTS ] keyspace_name
[ WITH REPLICATION '=' '{' keyspace_property '}']
[ AND DURABLE_WRITES '=' {true |false}]
Code:
CREATE KEYSPACE "Mykeyspace" with replication = {'class' : 'SimpleStrategy', 'replication_factor' : '3'};
Where,
- Mykeyspace: Name of the Keyspace
- replication: Properties of the Keyspace
Explanation: On the above query, the only property we configure here is for replication. In Cassandra, replication means storing multiple copies of data in different nodes, and each copy is called a replica.
Replica in Keyspace
We use a replica placement strategy algorithm to determine which nodes hold the replicas for a keyspace. There are two types of strategy.
- Simple Strategy: When we are dealing with only one data center, in that case, we use a simple strategy
- Network Topology Strategy: In the case of multiple datacenter network topology strategy is used
To define how many copies of data we want to store, change the replication factor property; on the above query is set as 3, and the class is SimplesStrategy for the replica placement strategy.
- Durable_writes: With this option, we can instruct Cassandra to use a commit log to update the current keyspace. This option is not mandatory to specify; it is set to true by default.
- If keyspace already exists in Cassandra, it will throw an error unless the “IF NOT EXISTS” option is used.
- We need to ensure that we created the keyspace accurately.
Once the execution is complete, a screenshot will display the creation of a keyspace called “Mykeyspace” with a Simple strategy and 3 replicas. The system generated all the remaining keyspaces.
How to Alter a Keyspace Cassandra?
To modify the replication factor, strategy name, or durable write property of an existing keyspace, use the “ALTER keyspace” command.
Syntax:
ALTER KEYSPACE [ IF EXISTS ] <"keyspace name"> WITH <"properties">
Where,
ALTER KEYSPACE “Mykeyspace” WITH replication: {‘class’ : ‘NetworkTopologyStrategy’, ‘replication_factor’ : 4};
Properties of Alter keyspace
Alter keyspace has two properties: replication and durable_writes:
- Replication: This option specifies the strategy for replica placement and the number of replicas
- Durable_writes: With this option, we can instruct Cassandra to use a commit log to update the current keyspace. This option is not mandatory to specify; it is set to true by default.
If keyspace does not exist in Cassandra, it will throw an error unless the “IF EXISTS” option is used.
Explanation:
- We are modifying the replication properties of the Mykeyspace keyspace by switching the class to the Network topology strategy and setting the replication factor to 4, as mentioned in the previous query.
- We can also alter the durable_writes property of a keyspace; given below is the CQL for the same.
Code:
ALTER KEYSPACE "Mykeyspace" WITH replication = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 4} AND DURABLE_WRITES=false;
Output:
The “ALTER KEYSPACE” command successfully changed the strategy name to Network topology, increased the replication factor to 4, and set the durable_writes option to false.
How to Delete a Keyspace?
To delete a keyspace, use the “DROP KEYSPACE” command, which will drop keyspace, including all the data, column families, user-defined types, and indexes from Cassandra. Before dropping, Cassandra takes care of capturing a snapshot of the keyspace. If keyspace does not exist in Cassandra, it will throw an error unless the “IF EXISTS” option is used.
Syntax:
DROP KEYSPACE [IF EXISTS]<"keyspace name">
i.e.
After successfully executing the command, Cassandra will delete the keyspace named ‘Mykeyspace,’ along with all its data and schema. Below is the snapshot where the error is thrown when we try to access the dropped keyspace.
Conclusion
It is much like a database in the RDBMS world; here, we have covered how to create a new keyspace, alter it or delete it from Cassandra. Keyspace helps us determine data replication on data nodes and other properties.
Recommended Articles
We hope that this EDUCBA information on “Keyspace in Cassandra” was beneficial to you. You can view EDUCBA’s recommended articles for more information.