Cassandra Collections: Set,List, and Map Tutorial 2019

In this Cassandra tutorial 2019 you will learn about cassandra Collection types. it is step by step procedures about Cassandra Collections which can further narrowed down as.

  1. Collections in Cassandra
  2. Types of Cassandra Collections
    1. SET
    2. LIST
    3. MAP

What is Cassandra Collections?

Cassandra collections are the good way to handle some kind of tasks. Like if a user wants to store multiple elements at the same time. But there are some limitations associated with Cassandra collections. Which are described below,

  • Cassandra collections unable to store data greater than 64 KB.
  • User must keep the size of collection smaller. As Cassandra traverse the whole collection on the time of query. So this way he prevents the query overhead to collections.
  • If a user tries to store more data greater than 64 KB in Cassandra collection. As a result Cassandra Collection retain on 64 KB. As well as data loss may occur. User can only query the 64 KB data in Collection.

Types of Cassandra Collection

There are three main types of Cassandra collections that the nosql Database (Cassandra) supports. These three types of Cassandra Collection are described below with examples.

Cassandra Set

This is the first type of cassandra Collections. Set (Cassandra Collection type) stores a number of elements as well as returns sorted list of elements when querying by user.

Syntax Cassandra Set Collection

Here is the syntax for Set (a collection type) that stores several email addresses for the Employee.

Create table University.Employee

(

id int,

Name text,

Email set<text>,

Primary key(id)

);

Example for “Set” Collection

The below is the screenshot where a table “Employee” is created with one column “Email” as a collection.

Cassandra Collection: Create table with Set Collection type
Cassandra Collection: Create table with Set Collection type

Here is the screenshot How to Insert data in the collection.

Cassandra Collection set: Insert Data
Cassandra Collection set: Insert Data

Syntax to insert data in set collection type

insert into University.Employee(id, Name, Email) values(l,'Obama',{'obama@gmail.com','obama@hotmail.com'});

How the retrieved data from collection shown?

Here is the screenshot shows that how to retieve data from cassandra collection “Set”

Cassandra Collection: Retrieve Data From Set
Cassandra Collection: Retrieve Data From Set

Syntax to Retrieve Data from SET Collection Type

select * from University.Employee;

Cassandra List

If the order of elements in result matters for user. this is place where cassandra list(another collection type) is used.

Below is the screenshot in which new column Roles of type list is added in table “Employee.”

Cassandra Collection : List
Cassandra Collection : List

Syntax for Cassandra Collection “List”

Alter table University.Employee add roles list<text>;

Below is the Screenshot where data is being inserted in column “roles”.

Cassandra Collection: Insert Data in List
Cassandra Collection: Insert Data in List

Syntax to Insert Data in List(A cassandra Collection type)

insert into University.Employee(id, Name, Email, roles) values(2,'michel',{'michel@hotmail.com',’ 'michel@gmail.com’},[‘Administrator’]);

Here is the screenshot that shows the current records in database table after new insertion.

Cassandra Collection: Retrieve Data from List
Cassandra Collection: Retrieve Data from List

Syntax to Retrieve Data From List(A cassandra Collection type)

select * from University.Employee;

Cassandra Map Collection

The third Cassandra collection type is map. This collection type in Cassandra is used for key value pair’s data type. The name MAP also truly manipulates the functionality of the collection type. That it maps one value to another.

For example, if you want to save roles of an employee with its prerequisite qualification. Cassandra Collection map is the best possible option.

Below screenshot shows how to create map Collection type in cassandra. In Current scenario roles with its prerequisite qualification are stored in cassandra.

Cassandra Collection: Create table with Map
Cassandra Collection: Create table with Map

Syntax to create the map Collections in Cassandra

Create table University.Employee2 ( id int, prereq map<text, text>, Primary key(id) );

Below screenshot shows how to insert data in map collection type in Cassandra.

Cassandra Collection: Insert data in Map
Cassandra Collection: Insert data in Map

Syntax to Insert data in Map collection type

insert into University.Course(id,prereq) values(1,'DataScience':'Database', 'Neural Network':'Artificial Intelligence'});

Below is the screenshot in which shows data inserted in map collection type in Cassandra.

Cassandra Collection: Retrieve Data from Map
Cassandra Collection: Retrieve Data from Map

Syntax to Retrieve Data From MAP (A Cassandra Collections type)

select * from University.Employee2;

Summary of Tutorial

This tutorial is about Cassandra Collection. And also explain Different type of Collections in cassandra. And also step by step procedure to create, insert data and retrieve data from these collections types. In short, To store multiple values, Cassandra collections are best choices.