Day 7: Cassandra Lab Practice – Collections (List, Set, Map)


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 duplicates
  • SET – unordered, no duplicates
  • MAP – 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

  1. Create a table with list, set, and map columns
  2. Insert at least 2 records with sample data
  3. Update collections (add and remove values)
  4. Query the full row and individual collections
  5. 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