Using the map type, you can store timestamp-related information in user profiles. A
map is a name and a pair of typed values that maps one thing to another.
As its name implies, a map maps one thing to another. A map is a name and a pair of
typed values. Using the map type, you can store timestamp-related information in
user profiles. Each element of the map is internally stored as one Cassandra column
that you can modify, replace, delete, and query. Each element can have an individual
time-to-live and expire when the TTL ends.
Procedure
-
Add a todo list to every user profile in an existing users table using the
CREATE TABLE or ALTER statement, specifying the map collection and enclosing the
pair of data types in angle brackets.
ALTER TABLE users ADD todo map<timestamp, text>;
-
Set or replace map data, using the INSERT or UPDATE command, and enclosing the
timestamp and text values in a map collection: curly brackets, separated by a
colon.
UPDATE users
SET todo =
{ '2012-9-24' : 'enter mordor',
'2014-10-2 12:00' : 'throw ring into mount doom' }
WHERE user_id = 'frodo';
-
Set a specific element using the UPDATE command, enclosing the timestamp of the
element in square brackets, and using the equals operator to map the value to
that timestamp.
UPDATE users SET todo['2014-10-2 12:00'] = 'throw my precious into mount doom'
WHERE user_id = 'frodo';
-
Use INSERT to specify data in a map collection.
INSERT INTO users (user_id, todo) VALUES ('frodo', { '2013-9-22 12:01' : 'birthday wishes to Bilbo', '2013-10-1 18:00': 'Check into Inn of Pracing Pony'}) ;
In Apache Cassandra™ 2.1.1 and later, you can add map elements using this syntax:
UPDATE users SET todo = todo + { '2013-9-22 12:01' : 'birthday wishes to Bilbo', '2013-10-1 18:00': 'Check into Inn of Pracing Pony'} WHERE user_id='frodo';
Inserting this data into the map replaces the entire map.
-
Delete an element from the map using the DELETE command and enclosing the
timestamp of the element in square brackets:
DELETE todo['2013-9-22 12:01'] FROM users WHERE user_id = 'frodo';
In Cassandra 2.1.1 and later, you can delete multiple map elements using this
syntax:
UPDATE users SET todo=todo - {'2013-9-22 12:01','2013-10-01 18:00:00-0700'} WHERE user_id='frodo';
-
Retrieve the todo map.
SELECT user_id, todo FROM users WHERE user_id = 'frodo';
The order of the map output depends on the type of the map.
-
Compute the TTL to use to expire todo list elements on the day of the
timestamp, and set the elements to expire.
UPDATE users USING TTL <computed_ttl>
SET todo['2012-10-1'] = 'find water' WHERE user_id = 'frodo';