We can store List, Set, Map into the Cassandra table's column. This is called Cassandra collections. In this lesson, I will show you how to work with List, Map, and Set using CQL.
Create Table With List Column:
CREATE TABLE email_list(
name text PRIMARY KEY,
email list<text
);
Insert email list:
INSERT INTO email_list(name, email)
VALUES ('Sarkar', ['sakar@gmail.com','mamun@yahoo.com'])
Adding item to list:
UPDATE email_list
SET email = email + ['test@artofcse.com']
WHERE name = 'Sarkar';
Delete Item from list:
UPDATE email_list
SET email = email - ['test@artofcse.com']
WHERE name = 'Sarkar';
Create Table With Set Column:
CREATE TABLE phone_set (
name text PRIMARY KEY,
phone set<varint>
);
Insert data into set:
INSERT INTO phone_set(name, phone)
VALUES ('Sarkar', {0124342543, 01823452343});
Adding Item to Set:
UPDATE phone_set
SET phone = phone + {01923445}
WHERE name = 'Sarkar';
Delete Item From Set:
UPDATE phone_set
SET phone = phone - {01923445}
WHERE name = 'Sarkar';
Create Table With Map:
CREATE TABLE address_map (
name text PRIMARY KEY,
address map<varchar, text>
);
Insert Data Into Map:
INSERT INTO address_map (name, address)
VALUES ('Sarkar', {'home' : 'Rangpur' , 'office' : 'Dhaka' } );
Add Item to Map:
UPDATE address_map
SET address = address + { 'current_address': 'Mirpur DOHS' }
WHERE name = 'Sarkar';
Remove Item From Map:
UPDATE address_map
SET address = address - { 'current_address' }
WHERE name = 'Sarkar';