As its name implies, a map maps one thing to another. A map is a name and a pair of typed values. One use case for the map type is storing timestamp-related information in user profiles.
To add a simple todo list to every user profile in an existing users table, use the CREATE TABLE or ALTER statement, specifying the map collection and enclosing the pair of data types in angle brackets. For example, enclose the timestamp data type and text data type in angle brackets:
ALTER TABLE users ADD todo map<timestamp, text>;
To set or replace map data, you can use the INSERT or UPDATE command. Enclose the timestamp and text values in a map collection: curly brackets, separated by a colon.
UPDATE users
SET todo =
{ '2012-9-24' : 'enter mordor',
'2012-10-2 12:00' : 'throw ring into mount doom' }
WHERE user_id = 'frodo';
You can also update or set a specific element using the UPDATE command. Enclose the timestamp of the element in square brackets and use the equals operator to map the value to that timestamp:
UPDATE users SET todo['2012-10-2 12:00'] = 'throw my precious into mount doom'
WHERE user_id = 'frodo';
To use INSERT to set or replace map data, specify data in a map collection.
INSERT INTO users (todo)
VALUES ( { '2013-9-22 12:01' : 'birthday wishes to Bilbo',
'2013-10-1 18:00' : 'Check into Inn of Prancing Pony' });
Inserting this data into the map replaces the entire map.
To delete an element from the map, use the DELETE command and enclose the timestamp of the element in square brackets:
DELETE todo['2012-9-24'] FROM users WHERE user_id = 'frodo';
Like the output of a query on a set, the order of the output of a map is based on the type of the map. To retrieve the todo map:
SELECT user_id, todo FROM users WHERE user_id = 'frodo';
Each element of the map is internally stored as one Cassandra column. Each element can have an individual TTL for instance. If you want elements of the todo list to expire the day of their timestamp, you can compute the correct TTL and set it as follows:
UPDATE users USING TTL <computed_ttl>
SET todo['2012-10-1'] = 'find water' WHERE user_id = 'frodo';