Tuesday, 24 July 2018

Cassandra Collection Data Types: List, Set, & Map

Cassandra Collection Data Types: List, Set, & Map

1. Objective

In our Cassandra journey, next is 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

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.
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 syntaxused to create table is.
  1. 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.
Example A.1 Creating the table.
Input:
  1. cqlsh> USE keyspace1;
  2. cqlsh:keyspace1> CREATE TABLE employee
  3. ... (EN int,
  4. ... NAME text,
  5. ... EMAIL LIST<text>,
  6. ... PRIMARY KEY(EN),
  7. ... );
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.
  1. cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3,....) VALUES('R1value1',['R1value1','R1value2','R1value3'...]['R1value11','R1value12','R1value13'...]...);
Example A.2: Cassandra insert table.
  1. cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
  2. ... VALUES(001,'Ayush',{'ayush@gmail.com'});
  3. cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
  4. ... VALUES(002,'Aarav',{'aarav@ymail.com'});
  5. cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
  6. ... VALUES(003,'Kabir',{'kabir@hotmail.com'});
iii. Update
UPDATE command in Cassandra is used to update the value in particular columns of the table. The syntax used to update is.
  1. cqlsh:<keyspace> UPDATE<table name>
  2. SET <column2>=<column2>+['value']
  3. where <column1>='some value';
Example A.3: Updating the table in Cassandra Collection.
  1. cqlsh:keyspace2>UPDATE college student
  2. SET EMAIL=EMAIL+['ayush.1@gmail.com']
  3. where EN=001;
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.
  1. cqlsh:<keyspace>> SELECT*FROM <table name>;
To verify the tables
Input:
  1. cqlsh:keyspace> SELECT*FROM employee;
Output after Example A.1:
EN                                            NAME                                                   EMAIL
                                  
(0 rows)
Output after Example A.2:
ENNAMEEMAIL
001                                   Ayushayush@gmail.com
002Aaravaarav@ymail.com
003Kabirkabir@hotmail.com
(3 rows)
Output after Example A.3:
ENNAMEEMAIL
001                                   Ayushayush.1@gmail.com
002Aaravaarav@ymail.com
003Kabirkabir@hotmail.com
(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.
  1. 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.
Example B.1 Creating the table.
Input:
  1. cqlsh> USE keyspace2;
  2. cqlsh:keyspace2> CREATE TABLE college students
  3. ... (EN int,
  4. ... NAME text,
  5. ... BRANCH SET<text>,
  6. ... PRIMARY KEY(EN),
  7. ... );
ii. Cassandra Insert
To insert value in a set INSERT INTO command is used with the following syntax.
  1. cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3...) VALUES('R1value',{'R1value1', 'R1value2',..},{ 'R1value11', 'R1value12',..}....);
Example B.2: Inserting in the table.

  1. cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
  2. ... VALUES(001,'Ayush',{'electrical engineering'});
  3. cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
  4. ... VALUES(002,'Aarav',{'Computer engineering'});
  5. cqlsh:keyspace2> INSERT INTO college student (EN, NAME, BRANCH)
  6. ... VALUES(003,'Kabir',{'Applied Physics'});
iii. Cassandra Update
A user can update the contents in a set using this syntax.
  1. cqlsh:<keyspace>>UPDATE <table name>
  2. SET <column2>=<column2>+['value']
  3. where <column1>='some value';
Example B.3: Updating the table.
  1. cqlsh:keyspace2>UPDATE college student
  2. SET BRANCH=BRANCH+['electrical and electronics']
  3. where EN=001;
iv. Verify 
To verify the contents of the table SELECT command is used. The syntax is.
  1. cqlsh:<keyspace>> SELECT*FROM <table name>;
To verify the tables
Input:
  1. 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:
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.
  1. 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:
  1. cqlsh:keyspace3>CREATE TABLE student
  2. ...(EN int,
  3. ... SUBJECT MAP(text,text),
  4. ... PRIMARY KEY(EN)
  5. ...);
ii. Insert
To insert value in a map INSERT INTO command is used with the following syntax.
  1. 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:
  1. cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
  2. ... VALUES(001,{'physics':'mathematics'})
  3. cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
  4. ... VALUES(002,{'operating system':'robotics'})
  5. cqlsh:keyspace3>INSERT INTO student(EN, SUBJECT)
  6. ... VALUES(003,{'power system':'machines'
iii. Update
A user can update the contents in a set using this syntax.
  1. cqlsh:<keyspace>>UPDATE <table name>
  2. SET <column2>=<column2>+['value1':'value2']
  3. where <column1>='some value';
Example C.3 Updating the table.
Input:
  1. cqlsh:keyspace3>UPDATE student
  2. SET SUBJECT=SUBJECT+['programming':'artificial intelligence']
  3. where EN=002;

iv. Verify
To verify the contents of the table SELECT command is used. The syntax is.
  1. cqlsh:<keyspace>> SELECT*FROM <table name>;
To verify the table we use the above syntax.
Input
  1. 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)
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.

0 comments:

Post a Comment