Primary Key:

  • In Cassandra, a table contain a number of rows.
  • Each row is represent by a primary key, also called the row key.
  • There are a number of columns in a row but the number of columns can vary in different rows.
  • For example, a table contain a one row that have three columns whereas another row in the same table can have 10 columns.
  • It is also important to note that in Cassandra, both column names and values have binary types.
  • That means column names can have binary values, such as strings, timestamps, or an integer, etc.
  • This is different from SQL databases, where each row in a SQL table has a fixed number of columns, and column names can only be text.
Create table person (student_id int primary key, fname text, lname text, 
                     dateofbirth timestamp, email text, phone text);

Partition Key:

  • The use of a partition key is to determine the partition in the cluster that stores that row.
  • When data is read or written from the cluster, a function called Partitioned.It is used to calculate the hash value of the partition key.
  • This hash value is used to calculate the partition in the row.
  • For example, rows whose partition key values range from 1000 to 1234 may reside in node A, and rows with partition key values range from 1235 to 2000 may reside in node B.
  • If a row having a partition key that contain hash value is 1233 then it will be stored in node A.
cassandra cluster

Clustering Key

  • The use of the clustering key is to store row data in a categorize order.
  • The arrangement of data is based on columns, which are involved in the clustering key.
  • This arrangement makes it well ordered to recover data using the clustering key.

Example: school system

  • To make these concepts clear, we will consider to Create a key space with replication strategy ‘SimpleStrategy’ and replication_factor 1.
reate keyspace Students_Details with replication = {‘class’ : ‘SimpleStrategy’, ‘replication_factor’:1};
  • Now switch to the students_details keyspace:
use students_details;
  • Check the number of tables present in the keyspace:
students_details> desc TABLES;
  • We will create a table, student , that contains general information about any student.
create table student (stuid int, avg_marks float, description text, 
primary key (stuid));
  • Type the following insert statements to enter some data into this table.
insert into student (stuid, avg_marks, description) values (1,25.5,’student 1′);
insert into student (stuid, avg_marks, description) values (2,35.5,’student 2′);
  • To view the details just inserted.
students_details> select * from student;

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,