Cassandra Collectiions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Cassandra Collections

Cassandra collections are used to handle tasks. You can store multiple elements in collection. There are three types of collection supported by Cassandra:

  • Set

  • List

  • Map


Set Collection

A set collection stores group of elements that returns sorted elements when querying.

Syntax:

  1. Create table table_name  

  2. (  

  3. id int,  

  4. Name text,  

  5. Email set,  

  6. Primary key(id)  

  7. );   

Example:

Let's take an example to demonstrate set collection. Create a table "employee" having the three columns id, name and email.

Cassandra Collection 1

The table is created like this:

Cassandra Collection 2

Insert values in the table:

  1. INSERT INTO employee (id, email, name)     

  2. VALUES(1, {'ajeetraj4u@gmail.com'}, 'Ajeet');    

  3. INSERT INTO employee (id, email, name)     

  4. VALUES(2,{'kanchan@gmail.com'}, 'Kanchan');   

  5. INSERT INTO employee (id, email, name)     

  6. VALUES(3, {'kunwar4u@gmail.com'}, 'Kunwar');  

Output:

Cassandra Collection 3


List Collection

The list collection is used when the order of elements matters.

Let's take the above example of "employee" table and a new column name "department" in the table employee.

Cassandra Collection 4

Now the new column is added. Insert some value in the new column "department".

Cassandra Collection 5

Output:

Cassandra Collection 6


Map Collection

The map collection is used to store key value pairs. It maps one thing to another. For example, if you want to save course name with its prerequisite course name, you can use map collection.

See this example:

Create a table named "course".

Cassandra Collection 7

Now table is created. Insert some data in map collection type.

Output:

Cassandra Collection 8