Collection Data Type in Apache Cassandra

Collection Data Type in Cassandra
In this article, we will describe the collection data type overview and CQL(Cassandra Query Language) query of each collection data type with an explanation. There are 3 types of collection data type in Cassandra.

1. SET
2. LIST
3. MAP 

Let discuss one by one.

1. SET:
A Set is a collection data where we can store a group of elements such that a user can have multiple Email address then to stored such type of data we used set collection data type which returns sorted elements when querying. It is a typed collection of unique values.



Syntax:
Create table keyspace-Name.Table-Name

(
  field 1 data_type 1,

  /*SET keyword is used to define collection data type.*/
  field 1 set data_type 2,  
  field 1 data_type 1,
  key_constraint if any,
 ); 

Step-1: To create keyspace used the following CQL query.

CREATE KEYSPACE keyspace1
    WITH replication = {'class': 'SimpleStrategy', 
                        'replication_factor' : 1};  

CQL query for using the keyspace1
Use keyspace1;

CREATE TABLE Employee
 ( 
  E_id int,
  E_name text,

  /* set syntax to define E_email as list collection data type. */
  E_email SET(text),  
  PRIMARY KEY(E_id),
 ); 

Step-2: The syntax for insertion using set collection data type.
Syntax:

INSERT INTO table_name(field 1, field 2, field 3..) 
            VALUES('field1_value', {'field2_value1', 'field2_value2', ..}, 
                                   { 'field3_value1', 'field3_value2', ..}....); 

CQL query for insertion using SET:

INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(100, 'Ayush Agarwal', {'ayush@gmail.com’, ’ayush2@gmail.com’});
INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(101, 'Arpan mishra', {'arpan@gmail.com’, ’arpan2@gmail.com’});
INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(102, 'Ashish Rana', {'ashish@gmail.com’, ’ashish2@gmail.com’}); 

Step-3: CQL query to retrieve the table data.

select * from Employee;

Output:




Figure – Cassandra-Table_set


2. LIST:
In list, one value can be stored multiple times. One of the rules in list collection data type that order of the elements cannot be changed. In list, once values are stored the elements get a particular index then values can be retrieved through these particular indexes. In list, element do not need to be unique and it can be duplicated.

Step-1: To create table used the following CQL query.


CREATE TABLE keyspace1.Employee
 ( 
  E_id int,
  E_name text,

  /* list syntax to define E_email as list collection data type. */
  E_email list text,  
  PRIMARY KEY(E_id),
 ); 

Step-2: The syntax for insertion using list collection data type.

Syntax:

INSERT INTO table_name(field 1, field 2, field 3..) 
            VALUES(' field1_value', ['field2_value1', 'field2_value2', ..], 
                                    [ 'field3_value1', 'field3_value2', ..]....); 

CQL query for insertion using LIST:

INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(101, 'Anmol', ['Anmol@gmail.com’, ’anmol1@gmail.com’]);
INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(102, 'Aakash tomar', ['Akash@gmail.com’, ’Akash2@gmail.com’]);
INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(103, 'Ashish paliwal', ['ashish@gmail.com’, ’ashish2@gmail.com’]);
INSERT INTO Employee (E_id, E_name, E_email) 
            VALUES(104, 'Ashish Rana', ['a1@gmail.com’, ’a2@gmail.com’]); 

Step-3: CQL query to retrieve the table data.

select * from Employee;

Output:




Figure – LIST table for above CQL query


3. MAP:
In MAP, typed collection of key-value pairs such that pair of values with a respective key name. MAP collection data type is ordered by unique keys.

Step-1: To create table used the following CQL query.

CREATE TABLE keyspace1.Activity
 ( 
  E_id int,
  E_name text,
  task map(timestamp, text),
  PRIMARY KEY(E_id),
 ); 

Step-2: The syntax for insertion using map collection data type.

Syntax:

INSERT INTO table_name(field 1, field 2, field 3..) 
            VALUES({'key1':'value1', 'key2':'value2'..........}); 

CQL query for insertion using MAP:

INSERT INTO Employee (E_id, E_name, Task) 
            VALUES(001, 'Vaibhav Nirwal', {'2019-09-28':'collection data type', 
                                                '2019-09-29':'map data type'});
INSERT INTO Employee (E_id, E_name, Task) 
            VALUES(002, 'Aksh Tomar', {'2019-09-28':'database created', 
                                       '2019-09-29':'creating table'}); 

Step-3: CQL query to retrieve the table data.

select * from Activity;

Output:




Figure – MAP table for above CQL query




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.