Creating Keyspaces and Tables in Cassandra

Definition of Keyspace

In Cassandra, a keyspace is a logical unit of data storage, similar to a database in MySQL. Each keyspace can contain multiple tables, which are all organized into column families.

Creating a Keyspace

To create a new keyspace, you need to use the CREATE KEYSPACE statement in CQL (Cassandra Query Language):

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

In this example, we create a keyspace named my_keyspace with a replication factor of 3.

Keyspace Options

When creating a new keyspace, you can specify the following options:

  • replication: Specifies the number of replicas and the distribution method for data. SimpleStrategy partitions all data to a single machine, while MultiStrategy distributes data across multiple machines, including different column families on the same machine.
  • durable_writes: Determines the duration for writing replicas. A value of 0 indicates that all writes must be persisted.
  • local_data_center: Specifies the key data center (dc) to control the data distribution for each partition.

Definition of Table

In Cassandra, a table is a logical unit of data storage. A table can contain multiple columns but does not support primary key uniqueness.

Creating a Table

To create a new table, you need to use the CREATE TABLE statement in CQL:

CREATE TABLE my_keyspace.my_table (
    id int,
    name text,
    PRIMARY KEY (id);
);

In this example, we create a table named my_table containing a primary key column (id) and two non-primary key columns (name).

Table Structure

When creating a new table, you can specify the following options:

  • PRIMARY KEY: Specifies the primary key for the table.
  • Clustering order: Selects the clustering method for primary key columns in the table. Using Default is the most common, which stores each row in ascending order on the machine.
  • Distributed: Selects whether non-primary key columns in the table need to be replicated.
  • column_type: Specifies the data type of the column (e.g., text, int, etc.)

Partition Key and Clustering Key

In Cassandra, the partition key is crucial for defining table structure and organizing data. The clustering key is the final primary key used to control how data is stored in a specific manner.

Partition Key

When creating a new table, you need to specify the partition key. This value will serve as the unique identifier for your table.

CREATE TABLE my_keyspace.my_table (
    id int,
    name text,
    PRIMARY KEY (id);
);

In this example, we define a primary key column (id).

Clustering Key

The clustering key is the final primary key used to control how data is stored in a specific manner. It is composed of multiple partition keys.

CREATE TABLE my_keyspace.my_table (
    user_id int,
    product_id int,
    PRIMARY KEY ((user_id, product_id));
);

In this example, we define a clustering primary key containing two columns (user_id and product_id).

Role of Partition Key and Clustering Key

The partition key and clustering key are key components of the Cassandra table structure. They help you organize your data and enable your applications to store, retrieve, and manage data more effectively.