✅ Day 7: Working with Collection Types – LIST, SET, MAP
๐ฏ Objective:
Learn how to use Cassandra’s collection types for storing multiple values in a single column:
LIST– ordered, allows duplicatesSET– unordered, no duplicatesMAP– key-value pairs
๐ 1. Creating Tables with Collections
➤ Example with all three:
CREATE TABLE test_lab.student_profile (
student_id uuid PRIMARY KEY,
name text,
subjects list<text>,
hobbies set<text>,
marks map<text, int>
);
๐งช 2. Inserting Collection Data
INSERT INTO test_lab.student_profile (
student_id, name, subjects, hobbies, marks
) VALUES (
uuid(), 'Alice',
['Math', 'Science'],
{'Reading', 'Music'},
{'Math': 90, 'Science': 88}
);
๐งผ 3. Updating Collections
➤ Add to List:
UPDATE test_lab.student_profile
SET subjects = subjects + ['English']
WHERE student_id = <UUID>;
➤ Add to Set:
UPDATE test_lab.student_profile
SET hobbies = hobbies + {'Painting'}
WHERE student_id = <UUID>;
➤ Add to Map:
UPDATE test_lab.student_profile
SET marks['English'] = 85
WHERE student_id = <UUID>;
๐️ 4. Removing from Collections
➤ Remove from List:
UPDATE test_lab.student_profile
SET subjects = subjects - ['Science']
WHERE student_id = <UUID>;
➤ Remove from Set:
UPDATE test_lab.student_profile
SET hobbies = hobbies - {'Reading'}
WHERE student_id = <UUID>;
➤ Remove a key from Map:
UPDATE test_lab.student_profile
SET marks = marks - {'Science'}
WHERE student_id = <UUID>;
๐ 5. Querying Collections
SELECT name, subjects, hobbies, marks
FROM test_lab.student_profile
WHERE student_id = <UUID>;
Note: Cassandra doesn't allow filtering by collection values unless you use
ALLOW FILTERING.
๐งช Day 7 Lab Tasks
- Create a table with
list,set, andmapcolumns - Insert at least 2 records with sample data
- Update collections (add and remove values)
- Query the full row and individual collections
- Observe how list order is preserved and set uniqueness is enforced
✅ Checklist
| Task | Done |
|---|---|
| Created table using list, set, and map | ✅ |
| Inserted and updated collection data | ✅ |
| Removed values from collection fields | ✅ |
| Queried and validated collections | ✅ |
Tags:
Cassandra