CodesJava

Easy learning with example program codes

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, {'xyz@gmail.com'});    
INSERT INTO volunteers (id, email)     
VALUES(2, {'yzx@gmail.com'}); 
INSERT INTO volunteers (id, email)     
VALUES(3, {'zxy@gmail.com'});

Output:

id	email
1	{'xyz@gmail.com'}
2	{'yzx@gmail.com'}
3	{'zxy@gmail.com'}

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, {'uvw@gmail.com'}, ['Medical']);

Output:

id	email	department
1	{'xyz@gmail.com'}	null
2	{'yzx@gmail.com'}	null
3	{'zxy@gmail.com'}	null
4	{uvw@gmail.com'}	['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:
error









Copyright © 2019 CodesJava DMCA.com Protection Status