Working with Partition in Cassandra
In this article, we are going to cover how we can make partition as per our requirements and how we can handle data into tables with partition key. Let’s discuss one by one.
Pre-requisite — Overview of Data Modeling
Partitioning for Data Modeling :
Partition is the small set of rows where you can say a table is split into a small subset of the table that shares the same partitioning key on the based of partition.
Let’s consider an example, where you want to store user login information like username, email, password, and Email ID, etc.
Column Name | Data Type |
---|---|
Usr_Name | text |
Usr_Email | text |
Usr_password | text |
Usr_ID | UUID |
Now, you will see how you can decide the partitioning key on the basis of user information access or you can say in which order you want to partition your data. Let’s write the cqlsh query for these specific requirements.
First, just create the keyspace by using the below cqlsh query as following.
CREATE KEYSPACE User_Info WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 2 };
To use the above-created keyspace used the following cqlsh command.
user User_Info;
Here, you will see how you can create the partition on the basis of the Usr_Info_by_email table. Let’s discuss one by one.
CREATE TABLE Usr_Info_by_email ( Usr_Name text, Usr_email text, Usr_password text, Usr_ID UUID, PRIMARY KEY(Usr_email) );
In Cassandra, table creation is just to handle the queries and depends on your application use cases. On Queries basis just define the table as per need of your application.
Now, To insert data into the table used the following cqlsh query.
Insert into Usr_Info_by_email (Usr_Name, Usr_email, Usr_password, Usr_ID) values ('Ashish', 'a@gmail.com', '123', uuid()); Insert into Usr_Info_by_email (Usr_Name, Usr_email, Usr_password, Usr_ID) values ('Aayush', 'ay@gmail.com', '124', uuid()); Insert into Usr_Info_by_email (Usr_Name, Usr_email, Usr_password, Usr_ID) values ('Ashish', 'as@gmail.com', '123', uuid()); Insert into Usr_Info_by_email (Usr_Name, Usr_email, Usr_password, Usr_ID) values ('Harsh', 'h@gmail.com', '125', uuid());
Output :
cassandra@cqlsh:user_data> select * from usr_info_by_email; usr_email | usr_id | usr_name | usr_password --------------+--------------------------------------+----------+-------------- h@gmail.com | fa91d89e-a401-4f4e-93b4-2b6282af75e1 | Harsh | 125 ay@gmail.com | a27c955f-ddc1-4134-a1fa-0f65fd175ce7 | Aayush | 124 a@gmail.com | 9982a5a9-14ff-4caa-a6d2-8d6790853e8c | Ashish | 123 as@gmail.com | aaafc38b-7184-4f8e-a325-b43fedd91828 | Ashish | 123 (4 rows)
Here, you can see clearly in the above example how you can access and partition your data on the basis of email.
Contact Us