Cassandra Collection Data Types: List, Set, & Map

Job-ready Online Courses: Dive into Knowledge. Learn More!

1. Objective

In our Cassandra journey, we will see Cassandra Collection Data Types tutorial. In this, we will know about Collection data type in Cassandra. These are data types just like arrays and structures in C, C++ etc.

Moreover, we will discuss this Cassandra Collection Data Types with the help of list, set, and map.
So, let’s start Cassandra Collection Data Types

Cassandra Collection Data Types

Cassandra Collection Data Types

Do you know about Cassandra Troubleshooting

2. Cassandra Collection Data Types

Collection data types in Cassandra are basically a unit in which multiple values are stored. Usually, a single variable defines Cassandra-CQL collection data type. This variable, in turn, contains multiple values.

There are a few collection data types, list, set and map. Many functions are performed on these Cassandra collection data types. These functions include create, insert, update and verify.
Cassandra Data Model | How Cassandra Stores Data

a. Cassandra List

In this data type, the values are stored in a form of a list. One value in this list is stored multiple times. There is one rule for the list data type.

The order of the elements cannot be changed. After storing the values in the list, the elements get a particular index. The values can be retrieved through these indexes.

i. Create
This Cassandra Create table is used to create a table with list data type, the user can apply CREATE TABLE command. The table can contain many columns. The syntax used to create table is.

cqlsh:<keyspace>>CREATE TABLE <table name>(column1 PRIMARY KEY,column2 list <data type>,column3 list <data type>,.....);

Creating a table ‘college students’ with columns, name, enrolment number and branch.
Best Cassandra Books to gain complete knowledge of Cassandra
Example A.1 Creating the table.

Input:

cqlsh> USE keyspace1;
cqlsh:keyspace1> CREATE TABLE employee
                             ... (EN int,
                             ... NAME text,
                             ... EMAIL LIST<text>,
                             ... PRIMARY KEY(EN),
                             ... );

ii. Insert
To insert elements in the table user can use INSERT INTO command. A comma separates all the values within square brackets. The syntax is.

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3,....) VALUES('R1value1',['R1value1','R1value2','R1value3'...]['R1value11','R1value12','R1value13'...]...);

Example A.2: Cassandra insert table.

cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(001,'Ayush',{'[email protected]'});
cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(002,'Aarav',{'[email protected]'});
cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(003,'Kabir',{'[email protected]'});

iii. Update
UPDATE command in Cassandra is used to update the value in particular columns of the table. The syntax used to update is.
Let’s revise Cassandra API

cqlsh:<keyspace> UPDATE<table name>
SET <column2>=<column2>+['value']
where <column1>='some value';

Example A.3: Updating the table in Cassandra Collection.

cqlsh:keyspace2>UPDATE college student
SET EMAIL=EMAIL+['[email protected]']
where EN=001;

Have a look at Cassandra Cluster

iv. Verify
To verify the contents of the table SELECT command is used. The syntax for Verify the table in Cassandra Collection Data Types is.

cqlsh:<keyspace>> SELECT*FROM <table name>;

To verify the tables
Input:

cqlsh:keyspace> SELECT*FROM employee;

Output after Example A.1:

EN                                            NAME                                                   EMAIL
                                  

(0 rows)
Explore Important Cassandra Features 
Output after Example A.2:

ENNAMEEMAIL
001                                   Ayush[email protected]
002Aarav[email protected]
003Kabir[email protected]

(3 rows)
Output after Example A.3:

ENNAMEEMAIL
001                                   Ayush[email protected]
002Aarav[email protected]
003Kabir[email protected]

(3 rows)

b. Cassandra Set

To store a group of the element, a user can use SET Cassandra collection data type. The elements in the set returns in a sorted order after execution.

i. Cassandra Create Table
To create a table with the set, a user can use CREATE command with the following syntax.

cqlsh:<keyspace> CREATE TABLE<table name> (column1 PRIMARY KEY, column2 set <data type>, column3 set <data type>.....);

Creating a table ‘college students’ with columns, name, enrolment number and branch.

You must know about Cassandra Monitoring Tools
Example B.1 Creating the table.
Input:

cqlsh> USE keyspace2;
cqlsh:keyspace2> CREATE TABLE college students
                             ... (EN int,
                             ... NAME text,
                             ... BRANCH SET<text>,
                             ... PRIMARY KEY(EN),
                             ... );

ii. Cassandra Insert
To insert value in a set INSERT INTO command is used with the following syntax.

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3...) VALUES('R1value',{'R1value1', 'R1value2',..},{ 'R1value11', 'R1value12',..}....);

Example B.2: Inserting in the table.
Let’s explore Cassandra Terminologies

cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
                        ... VALUES(001,'Ayush',{'electrical engineering'});
cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
                        ... VALUES(002,'Aarav',{'Computer engineering'});
cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
                        ... VALUES(003,'Kabir',{'Applied Physics'});

iii. Cassandra Update
A user can update the contents in a set using this syntax.

cqlsh:<keyspace>>UPDATE <table name>
SET <column2>=<column2>+['value']
where <column1>='some value';

Example B.3: Updating the table.

cqlsh:keyspace2>UPDATE college student
SET BRANCH=BRANCH+['electrical and electronics']
where EN=001;

Cassandra Applications | Why Cassandra Is So Popular?
iv. Verify 
To verify the contents of the table SELECT command is used. The syntax is.

cqlsh:<keyspace>> SELECT*FROM <table name>;

To verify the tables
Input:

cqlsh:keyspace2> SELECT*FROM COLLEGE STUDENTS;

Output after Example B.1:

EN                                            NAME                                                   BRANCH
                                  

(0 rows)
Output after Example B.2:

ENNAMEBRANCH
001                                   AyushElectrical Engineering
002AaravComputer Engineering
003KabirApplied Physics

(3 rows)
Output after Example B.3:

Do you know about Cassandra Manipulation Command

ENNAMEBRANCH
001                                   AyushElectrical and Electronics
002AaravComputer Engineering
003KabirApplied Physics

(3 rows)

c. Cassandra Map

Map in Cassandra collection data type, stores a key-value pair of elements.

i. Cassandra Create a Table
To create a table with map, user can use CREATE command with following syntax.

cqlsh:<keyspace> CREATE TABLE<table name> (column1 PRIMARY KEY, column2 map <type, data type>, column3 map <type, data type>.....);

Creating a table ‘student’ with 2 columns for Enrolment number and Subject.
Example C.1: Creating the table.
Input:

Have a look at Cassandra CQL Clauses

cqlsh:keyspace3>CREATE TABLE student
                       ...(EN int,
                       ... SUBJECT MAP(text,text),
                       ... PRIMARY KEY(EN)
                       ...);

ii. Insert
To insert value in a map INSERT INTO command is used with the following syntax.

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3...) VALUES('R1value',{'R1value1':'R1value1' ,R1value2:'R1value01',..},{ 'R1value11':'R1value011','R1value12':'R1value012',..}....);

Example C.2 Inserting in the table.
Input:

cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
                            ... VALUES(001,{'physics':'mathematics'})                
cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
                            ... VALUES(002,{'operating system':'robotics'})                       
cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
                            ... VALUES(003,{'power system':'machines'

iii. Update
A user can update the contents in a set using this syntax.

cqlsh:<keyspace>>UPDATE <table name>
SET <column2>=<column2>+['value1':'value2']
where <column1>='some value';

Example C.3 Updating the table.
Input:

cqlsh:keyspace3>UPDATE student
SET SUBJECT=SUBJECT+['programming':'artificial intelligence']
where EN=002;

Learn about Cassandra Architecture

iv. Verify
To verify the contents of the table SELECT command is used. The syntax is.

cqlsh:<keyspace>> SELECT*FROM <table name>;

To verify the table we use the above syntax.
Input

cqlsh:keyspace3>SELECT*FROM student;

Output after Example C.1

EN                                            SUBJECT
                                  

(0 rows)
Output after Example C.2

EN                                            SUBJECT
001                                   physics, mathematics
002Operating systems, robotics
003Power systems, machines

(3 rows)
Output after Example C.3

EN                                            SUBJECT
001                                   Physics, mathematics
002Programming, artificial intelligence
003Power systems, machines

(3 rows)

Test Your Cassandra Knowledge
So, this was all about Collection data types in Cassandra. Hope you like the article.

3. Conclusion

Hence, in this Cassandra Collection tutorial, we studied about collection data types in Cassandra. Also, we studied these Cassandra collection data types with the help of list, set and map Cassandra.

In the next article, we will know about Cassandra Shell Commands. Furthermore, if you have any query, feel free to ask through the comment section.

See also – 

Cassandra Interview Questions
For reference

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.

Leave a Reply

Your email address will not be published. Required fields are marked *