Cassandra Collections

To handle tasks, like to store multiple elements, collections are used in Cassandra. Collection can be of three types in Cassandra: Set, List or Map

Set: It stores a group of elements. Sorted elements are returned when querying. Example:

Creating a table:
Create table volunteers  
(  
id int,  
email set<text>,  
Primary key(id)  
);

Inserting values in the table:

INSERT INTO volunteers (id, email)     
VALUES(1, {'[email protected]'});    
INSERT INTO volunteers (id, email)     
VALUES(2, {'[email protected]'}); 
INSERT INTO volunteers (id, email)     
VALUES(3, {'[email protected]'});

Output:

id	email
1	{'[email protected]'}
2	{'[email protected]'}
3	{'[email protected]'}

List: When the order of elements matters, the list collection is used. Example:

Altering the table:
Alter table volunteers  
add department list<text>;

Inserting values in the table:

INSERT INTO volunteers (id, email, department)     
VALUES(4, {'[email protected]'}, ['Medical']);

Output:

id	email	department
1	{'[email protected]'}	null
2	{'[email protected]'}	null
3	{'[email protected]'}	null
4	{[email protected]'}	['Medical']

Map: To store a key-value pair, the map collection is used.

Example:

Creating a table:
Create table teachers  
(  
id int,  
teacher map<text, text>,  
Primary key(id)  
);

Output: After inserting some data in map collection type.

id	teacher
1	{‘History’: ‘Smith’, ‘Geography’: ‘Davison’}
Please follow and like us:
Content Protection by DMCA.com