Collection Data Type in Apache Cassandra


We shall view a Cassandra Collection Data Types tutorial along our Cassandra trip. In this, we will learn about Cassandra's Collection data type. These are data types in the same sense as arrays and structures in C, C++, etc.

In addition, we will talk about the Cassandra Collection Data Types using list, set, and map.

Consequently, let's begin with Cassandra Collection Data Types.

Collection Data Types for Cassandra

In Cassandra, a collection data type is essentially a storage container for several values. Typically, the Cassandra-CQL collection data type is defined by a single variable. This variable itself has a range of values.

List, set, and map are a few collection data types. On these Cassandra collection data types, numerous operations are carried out. The create, insert, update, and verify operations are among them.

a)Cassandra List

The values for this data type are kept in list form. This list contains several copies of a single value. For the list data type, there is only one rule.

The elements cannot be modified in order. The elements are given a specific index after the values have been stored in the list. These indexes can be used to obtain the values.

i) Create Table

In Cassandra, an individual can use the CREATE TABLE command to create a table with a list data type. There may be numerous columns in the table. The table-creation syntax is.

cqlsh:<keyspace>>CREATE TABLE <table name>(column1 PRIMARY KEY,column2 list <data type>,column3 list <data type>,.....);

Constructing a table with the columns name, enrollment number, and branch for "college students."

cqlsh> USE keyspace1;
cqlsh:keyspace1> CREATE TABLE employee
                            ... (EN int,
                            ... NAME text,
                            ... EMAIL LIST,
                            ... PRIMARY KEY(EN),
                            ... );

Output

EN

NAME

EMAIL

ii) Insert

The INSERT INTO command can be used by the user to add components to the table. Each value included in square brackets is separated by a comma. The syntax is −

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3,....) VALUES('R1value1',['R1value1','R1value2','R1value3'...]['R1value11','R1value12','R1value13'...]...);

Example

cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(001,'hardik',{'hardi@gmail.com'});
cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(002,'Ajites',{'ajit@mail.com'});
cqlsh:keyspace1> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(003,'Pushpa',{'tears@mail.com'});

Output

EN

NAME

EMAIL

001

hardik

hardi@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

iii) Update

Cassandra's UPDATE command is used to update the value of certain table columns. Update syntax is as follows.

cqlsh:<keyspace> UPDATE<table name>
SET <column2>=<column2>+['value']
where <column1>='some value';

Example

cqlsh:keyspace2>UPDATE college student
SET EMAIL=EMAIL+['hardikgupta.1@gmail.com']
where EN=001;

Output

EN

NAME

EMAIL

001

hardik

hardikgupta.1@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

b) Cassandra Set

A user can use the SET Cassandra collection data type to store a collection of elements. After execution, the components of the set are returned in a sorted manner.

i. Create Table

A user can use the Construct command with the following syntax to create a table that contains the set.

cqlsh:<keyspace> CREATE TABLE<table name> (column1 PRIMARY KEY, column2 set <data type>, column3 set <data type>.....);

Example

Constructing a table with the columns name, enrollment number, and branch for "college students."

cqlsh> USE keyspace2;
cqlsh:keyspace2> CREATE TABLE employee
                            ... (EN int,
                            ... NAME text,
                            ... EMAIL LIST<text>,
                            ... PRIMARY KEY(EN),
                            ... );

Output

EN

NAME

EMAIL

ii. Cassandra Insert

The INSERT INTO command is used with the following syntax to insert a value into a set.

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3...) VALUES('R1value',{'R1value1', 'R1value2',..},{ 'R1value11', 'R1value12',..}....);

Example

>
cqlsh:keyspace2> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(001,'hardik',{'hardi@gmail.com'});
cqlsh:keyspace2> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(002,'Ajites',{'ajit@mail.com'});
cqlsh:keyspace2> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(003,'Pushpa',{'tears@mail.com'});

Output

EN

NAME

EMAIL

001

hardik

hardi@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

iii. Cassandra Update

A user can update the contents in a set using this syntax.

cqlsh:<keyspace>>UPDATE <table name>
SET <column2>=<column2>+['value']
where <column1>='some value';

Example

cqlsh:keyspace2>UPDATE college student
SET EMAIL=EMAIL+['hardikgupta.1@gmail.com']
where EN=001;

Output

EN

NAME

EMAIL

001

hardik

hardikgupta.1@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

c)Cassandra Map

A pair of key-value items are stored in a map, a Cassandra collection data type.

i. Create Table

The user can use the Construct command using the following syntax to create a table with a map.

cqlsh:<keyspace> CREATE TABLE<table name> (column1 PRIMARY KEY, column2 map <type, data type>, column3 map <type, data type>.....);

Constructing a table with the columns name, enrollment number, and branch for "college students."

cqlsh> USE keyspace3;
cqlsh:keyspace3> CREATE TABLE employee
                            ... (EN int,
                            ... NAME text,
                            ... EMAIL LIST,
                            ... PRIMARY KEY(EN),
                            ... );

Output

EN

NAME

EMAIL

ii. Insert

The INSERT INTO command is used with the following syntax to insert a value into a map.

cqlsh:<keyspace>> INSERT INTO <table name>(column1, column2, column3...) VALUES('R1value',{'R1value1':'R1value1' ,R1value2:'R1value01',..},{ 'R1value11':'R1value011','R1value12':'R1value012',..}....);

Example

cqlsh:keyspace3> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(001,'hardik',{'hardi@gmail.com'});
cqlsh:keyspace3> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(002,'Ajites',{'ajit@mail.com'});
cqlsh:keyspace3> INSERT INTO college student (EN, NAME, EMAIL)
                        ... VALUES(003,'Pushpa',{'tears@mail.com'});

Output

EN

NAME

EMAIL

001

hardik

hardi@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

iii. Update

Using this technique, a user can modify a set's contents.

cqlsh:<keyspace>>UPDATE <table name>
SET <column2>=<column2>+['value1':'value2']
where <column1>='some value';

Example

cqlsh:keyspace3>UPDATE college student
SET EMAIL=EMAIL+['hardikgupta.1@gmail.com']
where EN=001;

Output

EN

NAME

EMAIL

001

hardik

hardikgupta.1@gmail.com

002

Ajites

ajit@mail.com

003

Pushpa

tears@mail.com

Conclusion

These were the three collection Data types in Apache Cassandra. Task management is made easy via Cassandra collections. Collections allow for the storage of numerous items.

Updated on: 06-Apr-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements