/    /  Cassandra – Collection Types

Cassandra – Collection Types

Collections provide the simplest way to handle multiple tasks.The maximum size of an item in collection is 64K. Also it is advisable to keep collections small to avoid delay during query execution.Even though if you insert more than 64K data that will result into dataloss, because only 64K is queryable.

SET Type

Set Type will help us the situations when we need to include multiple emails.

Example:

Create a table
CREATE TABLE users (
user_id text PRIMARY KEY,
first_name text,
last_name text,
emails set<text>
);

Insert data into the set. Set values must be unique.

INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('1020', 'cassandra', 'julie', {'c@emails.com', 'julie@email.com'});

If you want to add one more email

UPDATE users SET emails = emails + {'casa@email.com'} WHERE user_id = '1020';

List Type

List Type can be used when data elements required in an order.It will return the values as per the index value in the list.

Example:

Add the list of technologies known by users to a table by adding a column technologies of the list type to the users table.

ALTER TABLE users ADD technologies list<text>;

Use the UPDATE command to insert values into the list.

UPDATE users SET technologies = ['java', 'C++'] WHERE user_id = '1020';

MAP

Map type is used to store key value pairs which are mapped.Each element of the map can be modified, replace, delete and query.

Example:

Let us add a column which is map type to show events.

ALTER TABLE users ADD todo map<DATE, text>;

Using the INSERT or UPDATE command lets add the map data

UPDATE users
SET todo =
{ 
'2016-08-22' : 'Seminar Event',
'2016-10-10' : 'Auditing'
}
WHERE user_id = '1020';