Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra CQL collection


May 17, 2021 Cassandra


Table of contents


CQL provides the ibility to use TheCollection data type. U sing these collection types, you can store multiple values in a single variable. This chapter describes how to use Colleges in Cassandra.

List

List is used in the following cases

  • The order of the elements is maintained, and
  • The value will be stored more than once.

You can use the index of elements in the list to get the value of the list data type.

Create a table with List

Here's an example of creating a sample table with two columns (name and e-mail). To store multiple e-mail messages, we use lists.

cqlsh:tutorialspoint> CREATE TABLE data(name text PRIMARY KEY, email list<text>);

Insert data into the list

When you insert data into an element in the list, enter all the values separated by commas in square brackets like the following.

cqlsh:tutorialspoint> INSERT INTO data(name, email) VALUES ('ramu',
['[email protected]','[email protected]'])

Update the list

Here's an example of updating the list data type in a table called data. Here, we're adding another email to the list.

cqlsh:tutorialspoint> UPDATE data
... SET email = email +['[email protected]']
... where name = 'ramu';

Verify

If you validate the table with the SELECT statement, you get the following results:

cqlsh:tutorialspoint> SELECT * FROM data;

 name | email
------+--------------------------------------------------------------
 ramu | ['[email protected]', '[email protected]', '[email protected]']

(1 rows)

Set

Set is the type of data used to store a set of elements. The elements of the collection are returned in sort order.

Create a table with Set

The following example creates a sample table with two columns (name and phone). For storing multiple phone numbers, we use collections.

cqlsh:tutorialspoint> CREATE TABLE data2 (name text PRIMARY KEY, phone set<varint>);

Insert data into the collection

When you insert data into an element in a collection, enter all the values separated by commas in parentheses, as shown below.

cqlsh:tutorialspoint> INSERT INTO data2(name, phone)VALUES ('rahman',    {9848022338,9848022339});

Update the collection

The following code shows how to update a collection in a table named data2. Here, we are adding another phone number.

cqlsh:tutorialspoint> UPDATE data2
   ... SET phone = phone + {9848022330}
   ... where name = 'rahman';

Verify

If you validate the table with the SELECT statement, you get the following results:

cqlsh:tutorialspoint> SELECT * FROM data2;

   name | phone
--------+--------------------------------------
 rahman | {9848022330, 9848022338, 9848022339}

(1 rows)

Map

A map is the type of data that is used to store key-value pairs of elements.

Use Map to create a table

The following example shows how to create a sample table with two columns (name and address). In order to store multiple address values, we use map.

cqlsh:tutorialspoint> CREATE TABLE data3 (name text PRIMARY KEY, address
map<timestamp, text>);

Insert data into the map

When you insert data into elements in the map, enter all the keys: value pairs, separated by commas between commas, as shown below.

cqlsh:tutorialspoint> INSERT INTO data3 (name, address)
   VALUES ('robin', {'home' : 'hyderabad' , 'office' : 'Delhi' } );

Update the collection

The following code shows how to update the map data type in a table named data3. Here, we change the value of the key office, that is, we change the office address of a person named Robin.

cqlsh:tutorialspoint> UPDATE data3
   ... SET address = address+{'office':'mumbai'}
   ... WHERE name = 'robin';

Verify

If you validate the table with the SELECT statement, you get the following results:

cqlsh:tutorialspoint> select * from data3;

  name | address
-------+-------------------------------------------
 robin | {'home': 'hyderabad', 'office': 'mumbai'}

(1 rows)