Javatpoint Logo
Javatpoint Logo

Role of Keys in Cassandra

In this tutorial, we'll talk about keys' significance, functionality, and differences from relational databases. Let's have a look at how keys work in the database to sort and organize data into columns and rows.

There are three types of keys in Cassandra:

  • Simple Primary Key
  • Composite Key
  • Using a compound primary key

Simple Primary Key

A basic primary key uses the column name as the partition key in one column. In this instance, the partition key is the only element of the primary key. When requesting data from the table, only the primary key can be given. Data stored using a straightforward primary key, such as a basic id or text primary key, can be quickly inserted and retrieved under this structure if multiple column values can distribute the partitions among many nodes.



In the above code, we created a table to store the data of an employee of an organization. We made the employee's id as the primary column to uniquely identify.

Composite Key

With the help of the key, we may use the Cassandra composite partition key for more sorted rows. To further understand, let's use an example. In a table with a composite partition key, Cassandra enables the use of several columns as the partition key. Compared to a simple partition key, a composite partition key indicates where data will be kept with many columns when the amount of data is too enormous to fit in a single partition. A composite partition key is useful when hotspotting or writing data congestion are problems since it divides data into parts with several columns. Users can also return sorted data and divide results across different partitions for queries.



In the above code, Emp_firstName is the primary key which is also called the composite partition key for the table emp_data.



In the above code, we use Emp_firstName as the primary key and Emp_domain in the primary key. We inserted four rows with data of employees.

We will get the data in the sorted order of Emp_domain.

So we will get the data in the order of Ayush -> Shivani -> Gopal -> Mansi

That is in the sorted order of their domain.

Using a Compound Primary Key

Create numerous columns with a compound primary key so you may query them and use them to provide sorted results.

Let's use the Emp_data table as an example and denormalize the data there. Use two or more columns as the main key to construct a table with a compound primary key.


Now we can add a clause WITH CLUSTERING ORDER BY to sort the data of the table according to the employee's rating in the descending order.



In the above code, we inserted data of four employees and then used the clause WITH CLUSTERING ORDER BY in descending order of emp_rating.

So it will sort the table in descending order of their rating. When we run the code, we will get the order of the table as Gopal -> Mansi -> Ayush -> Shivani.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA