With no single point of failure and the ability to handle massive volumes of data across numerous commodity servers, Apache Cassandra is a distributed database that is highly performant and scalable. It belongs to the NoSQL database family.

The Ultimate Data Science Job Guarantee Program

6 Month Data Science Course With a Job GuaranteeJoin Today
The Ultimate Data Science Job Guarantee Program

Features of Cassandra

  • Transaction support
  • No-failure architecture
  • Flexible storage of data
  • Ease of data distribution
  • Linear-scale performance that is fast
  • Scalability that is elastic in nature
  • Faster writes

In this article, we will learn about what a keyspace is in Cassandra and its various operations.

What Is Keyspace?

An object called a keyspace is used to store user-defined types and column families. A keyspace is similar to an RDBMS database in that it includes indexes, user-defined types, indexes, replication factors, strategy, column families, data center awareness, etc.

Let us look at the various operations of keyspace in Cassandra.

Cassandra Keyspace Operations

The different Keyspace Operations in Cassandra include -

  • Creating a Keyspace
  • Altering a Keyspace
  • Dropping a Keyspace

Let us now look at each of them in detail. 

1. Cassandra Create Keyspace

In Cassandra, a namespace that specifies data replication on nodes is known as a Keyspace. Each node in a cluster has its own Keyspace.

Below is the syntax of Creating a Keyspace in Cassandra.

Syntax of Cassandra Create keyspace

CREATE KEYSPACE <identifier> WITH <properties of keyspace>

Complete Syntax With Properties:

CREATE KEYSPACE “KeySpace Name”

WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of replicas’};

CREATE KEYSPACE “KeySpace Name”

WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of replicas’}

AND durable_writes = ‘Boolean value’;

We will now look at the properties of Keyspace in Cassandra.

Two Properties of Cassandra Create Keyspace

There are two main properties of Cassandra:

  • durable_writes
  • Replication 

Let us discuss them in detail now.

1. Replication

The Replica Placement approach and desired number of replicas are specified in the Replication option. 

The replica placement strategies are all listed below:

  • Old Network Topology Strategy - This approach for legacy replication.
  • Network Topology Strategy - You can independently configure the replication factor for each data center using this option.
  • Simple Strategy - Assigns the cluster a basic replication factor.

An illustration of constructing a KeySpace is provided below. 

Post Graduate Program In Data Science

The Ultimate Ticket To Top Data Science Job RolesExplore Course
Post Graduate Program In Data Science

Example

cqlsh.> CREATE KEYSPACE simplilearn

WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 2};

Here, a KeySpace called simplilearn is being created.

  • We are employing Network Topology Strategy, the second replica placement method.
  • Additionally, we have set the replication factor to 2 replicas.
Verification

Using the command Describe, you may determine whether or not the table has been created. This command will display all created keyspaces as seen below if you use it over keyspaces.

cqlsh> DESCRIBE keyspaces;

simplilearn system system_traces

2. durable_writes

A table's durable_writes property can be changed from its default value of true to false. This property cannot be set to simplex strategy.

Example

cqlsh> CREATE KEYSPACE test

... WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 }

... AND DURABLE_WRITES = false;

Verification

By querying the System Keyspace, we can determine whether the test KeySpace's durable writes attribute was set to false. We can get a list of all KeySpaces and their properties with the following query -

cqlsh> SELECT * FROM system_schema.keyspaces;

keyspace_name | durable_writes | strategy_class | strategy_options

----------------+----------------+------------------------------------------------------+----------------------------

test | False | org.apache.cassandra.locator.NetworkTopologyStrategy | {"datacenter1" : "3"}

simplilearn | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor" : "4"}

system | True | org.apache.cassandra.locator.LocalStrategy | { }

system_traces | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor" : "2"}

We can see the test's durable writes attribute here. The value of KeySpace was false.

So, how do we create Keyspace? Let us see how.

Using a Keyspace

The keyword USE can be used to access a newly established KeySpace.

The following syntax can be used to do so -

Syntax

USE <identifier>

Example

We are utilizing the KeySpace tutorialspoint in the example that follows -

cqlsh> USE simplilearn;

cqlsh:tutorialspoint>

Let us now look at how to alter KeySpace.

Free Course: Introduction to Data Science

Learn the Fundamentals of Data ScienceEnroll Now
Free Course: Introduction to Data Science

2. Altering a KeySpace

We can change KeySpace's durable_writes and replica count using the ALTER KEYSPACE command. 

The syntax for this command is shown below.

Syntax

ALTER KEYSPACE <identifier> WITH <properties>

Detailed Syntax

ALTER KEYSPACE “KeySpace Name”

WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of replicas’};

Let us now explore the properties of Alter KeySpace.

Properties of Alter Keyspace

The properties of CREATE KEYSPACE and ALTER KEYSPACE are identical.

They are:

  • Replication
  • durable_writes

Let us explore them in a detailed manner.

1. Replication

The replication option provides the desired number of replicas as well as the replica placement strategy.

The replica placement strategies are all listed below:

  • Old Network Topology Strategy - This approach for legacy replication.
  • Network Topology Strategy - You can independently configure the replication factor for each data center using this option.
  • Simple Strategy - Assigns the cluster a basic replication factor.

An illustration of constructing a KeySpace is provided below. 

Example

cqlsh.> ALTER KEYSPACE simplilearn

WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 2};

2. durable_writes

We can instruct Cassandra on whether to use commitlog for updates on the current KeySpace or not using this option. This option is optional and has the value true by default.

An example of changing a KeySpace is provided below.

Example

Here, we're changing the SimpliLearn KeySpace.

The replication factor is being changed from 1 to 3.

cqlsh.> ALTER KEYSPACE simplilearn

WITH replication = {'class':'NetworkTopologyStrategy', 'replication_factor' : 3};

Let us now see how we alter durable_writes.

Altering durable_writes

SELECT * FROM system.schema_keyspaces;

keyspace_name | durable_writes | strategy_class | strategy_options

----------------+----------------+------------------------------------------------------+----------------------------

test | False | org.apache.cassandra.locator.NetworkTopologyStrategy | {"datacenter1":"3"}

tutorialspoint | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"4"}

system | True | org.apache.cassandra.locator.LocalStrategy | { }

system_traces | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"2"}

(4 rows)

ALTER KEYSPACE test

WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3}

AND DURABLE_WRITES = true;

We have learned how to create, use and alter Keyspace in Cassandra. Now let us see how to drop a Cassandra Keyspace.

3. Cassandra Drop Keyspace

The command DROP KEYSPACE can be used to delete or drop a KeySpace. 

The syntax below demonstrates dropping a KeySpace.

Syntax

DROP KEYSPACE “KeySpace name”

Example

The keyspace simplilearn is deleted by the code that follows.

cqlsh> DROP KEYSPACE simplilearn;

Verification

Use the command Describe to examine the keyspaces, then see if the table has been dropped as indicated below.

cqlsh> DESCRIBE keyspaces;

system system_traces

We won't be able to find the keyspace simplilearn in the list of keyspaces because we dropped it.

Enroll in the PG Program in Data Science to learn over a dozen of data science tools and skills, and get exposure to masterclasses by Purdue faculty and IBM experts, exclusive hackathons, Ask Me Anything sessions by IBM.

Master Cassandra Keyspace With Simplilearn

In this article, we learned about keyspace in Cassandra and its various operations like creating, altering, and dropping a keyspace in Cassandra. We also discussed the various properties involved in these operations, i.e., durable_writes and Replication, that are part of the create and alter keyspaces operations.

To gain further knowledge on keyspaces, dive deeper into Cassandra's world, and level up as a data scientist, check out Simplilearn's Data Science Certification Program. This Data Science program features masterclasses, hackathons, and Ask Me Anything sessions hosted by the experienced faculty at Purdue University, and the experts at IBM.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.