cassandra
  1. cassandra-create-table

Creating a Cassandra Table

Cassandra is a NoSQL database that is designed to handle large amounts of data and provide high availability with no single point of failure. In order to store data in Cassandra, we need to create a table to define the structure of that data.

Syntax

The basic syntax for creating a Cassandra table is as follows:

CREATE TABLE <table_name> (
    <column_name1> <dataype> [PRIMARY KEY | CLUSTERING ORDER],
    <column_name2> <dataype>,
    ...
    PRIMARY KEY (<partition_key_column>, <clustering_column>)
)
[WITH <property_name> = <property_value>];

Example

Suppose we want to create a table to store information about users in a social media platform. We want to store the user's name, email, age, location, and a unique identifier. We can create a table to store this data in Cassandra as follows:

CREATE TABLE users (
    id uuid PRIMARY KEY,
    name text,
    email text,
    age int,
    location text
);

Output

The output of the above example is a new table called 'users' created in Cassandra.

Explanation

In the example above, we create a table called 'users' by defining the columns to include in the table. We start by specifying the primary key, which consists of a partition key column and optional clustering columns. We use the 'id' column as our primary key, which is of type UUID.

The remaining columns in the table are defined in the same way, specifying the column name and data type. In this case, we create columns to store the user’s name, email, age, and location.

Use

Creating a table in Cassandra is essential for storing data in a structured manner. Tables can be created to store a wide variety of data types, from simple data types to complex data types like collections and user-defined types. Tables can also be configured with properties to control how data is stored and retrieved.

Important Points

  • Define the columns of your table carefully. The partition key determines how data is distributed across nodes in the cluster, while clustering columns define how data is sorted within a partition.
  • Choose your primary key wisely. The primary key should be unique and well-distributed to ensure good performance and even distribution of data across nodes.
  • Consider adding additional properties to your table to optimize performance. Options such as 'compaction', 'caching', and 'compression' can affect how data is stored and retrieved, and can have a big impact on performance.

Summary

In this tutorial, we learned about how to create a table in Cassandra and defined the structure of the data to be stored in the table. We saw the basic syntax for creating a Cassandra table and learned how to define columns, primary keys, and clustering columns. Finally, we discussed some important considerations when creating a table in Cassandra and how these can affect performance.

Published on: