cassandra
  1. cassandra-create-keyspace

Cassandra Keyspace

A keyspace in Cassandra is a namespace that defines data replication on nodes. Each keyspace contains one or more tables which hold the actual data. The keyspace defines the replication factor and other options for the tables that it contains.

Syntax

The basic syntax for creating a keyspace in Cassandra is as follows:

CREATE KEYSPACE keyspace_name
WITH replication = {
   'class': 'SimpleStrategy',
   'replication_factor': 'number_of_replicas'
};

Here, keyspace_name is the name of the keyspace, class is the replication strategy class, and replication_factor is the number of replicas to store in the cluster.

Example

Let's say we want to create a keyspace called mydb with a replication factor of 3. We can use the following CQL command:

CREATE KEYSPACE mydb
WITH replication = {
   'class': 'SimpleStrategy',
   'replication_factor': '3'
};

Output

The output of this command will be CREATE KEYSPACE.

Explanation

In the above example, we're creating a keyspace called mydb using the SimpleStrategy replication strategy class. The replication_factor is set to 3, which means that the data will be replicated to 3 different nodes in the cluster. This ensures that the data is always available even if one or more nodes fail or become unavailable.

Use

The main use of a keyspace in Cassandra is to define data replication and placement rules for tables within a cluster. By choosing an appropriate replication strategy and replication factor, you can ensure that your data is secure, highly available, and fault-tolerant.

Important Points

  • Keyspaces define data replication and placement rules for tables within a cluster.
  • Keyspaces contain one or more tables that hold actual data.
  • SimpleStrategy and NetworkTopologyStrategy are commonly used replication strategies.
  • Replication factor determines the number of replicas of data stored across nodes in a cluster.
  • Keyspaces can be altered or dropped using CQL commands.

Summary

In this tutorial, we learned about Cassandra keyspace and how it is used to define data replication and placement rules for tables within a cluster. We saw how to create a keyspace using the CREATE KEYSPACE command with a specified replication strategy and replication factor. Replication strategies and replication factor are key components of a keyspace. It is fundamental to choose the optimal replication strategy and replication factor for optimal use of Cassandra functionalities.

Published on: