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.
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.
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.
- Now switch to the students_details keyspace:
- Check the number of tables present in the keyspace:
- We will create a table, student , that contains general information about any student.
- Type the following insert statements to enter some data into this table.
- To view the details just inserted.