Collection Data Types

CQL also provides collection data types. Following list provides different data types available in cassandra.

  1. List - Collection of one or more ordered elements.
  2. map - Collection of key-value pairs.
  3. set - Collection of one or more elments.

List

List is used to maintain the order of elements allowing duplicates.

  • Creating a Table with List
CREATE TABLE employee(id int PRIMARY KEY, name text, email list<text>);
  • Inserting Data into List
INSERT INTO employee(id, name, email) VALUES (1, 'john',
['[email protected]','[email protected]'])
  • Updating a List
UPDATE employee SET email = email +['[email protected]']
WHERE name = 'john';

SET

SET is used to store group of data and the elements will be returned in sorted order.

  • Creating a Table with Set
CREATE TABLE employee2(id int PRIMARY KEY, name text, mobile set<varint>);
  • Inserting Data into Set
INSERT INTO employee2(id, name, mobile) VALUES (1, 'kevin',
{9999911122, 9998855664})
  • Updating a List
UPDATE employee2 SET mobile = mobile +{9898989898}
WHERE name = 'kevin';

MAP

Map is used to store key-value pair of elements.

  • Creating a Table with Map
CREATE TABLE employee3(id int PRIMARY KEY, name text, address map<text, text>);
  • Inserting Data into Map
INSERT INTO employee3(id, name, address) VALUES (1, 'smith',
{'home':'san jose','work':'chicago'})
  • Updating a Map
UPDATE employee3 SET address = address +{'permanent':'New york'}
WHERE name = 'smith';