Cassandra Collection

 Cassandra Collections:

The collections of Cassandra are a useful method to handle activities.  In collections, several components can be kept. Though, Cassandra collections have limits.

  • Unable to save data in excess of 64KB the Cassandra collection.
  • Hold a smaller collection to avoid a query collection overhead since the complete collection has to be passed through.
  • When more than 64 KB of data is stored in the collection, it can only be queried by 64 KB and result in data loss.

There are three types of collections that Cassandra supports.

  • Cassandra Set
  • Cassandra List
  • Cassandra Map
  • Cassandra Tuple
  • Cassandra Nested Collections

Cassandra Set:

Unique values of the same data type collected in a group.

When querying, a Set holds a group of elements and returns sorted elements.

Syntax:

Below is the syntax to store a set collection of various departments of an Employee.

Create table Firm.Employee
(
id int,
Name text,
Dept Text,
Primary key(id)
);

Example

Below is the screenshot in which the "Employee" table is formed as collection with "Dept" field.

Cassandra Collection

Here is the screenshot in which data is entered into the collection.

Cassandra Collection
insert into Firm.Employee (id,Name,Dept) values(5,'Ram',{’IT’,’BD’});

Cassandra List:

A collection of non-unique items of the same data type that is arranged in a certain order. The List is used when the order of the components is crucial.

Below is a screenshot of adding a new column i.e, Salary to the Employee table.

Cassandra Collection

Below is a screenshot of data getting inserted into in Employee table i.e, Salary.

Cassandra Collection
insert into Firm.Employee(id, Name, dept, salary) values(5,'Ram', IT ,40000);

Below is a glimpse at the present status of the Table in Keyspace after addition of new column and its value.

Cassandra Collection

 

Cassandra Map:

The map is a kind of collection used to hold pairs of key value. As its name indicates that one item is mapped to another. Key value pairs with unique keys and with related data types of both keys and values.

For instance, we may store with the name DeptProjects, if you want to save the wise departmental project of a person who works in several departments.

Below is a screenshot where map type is created for department and its department wise projects.

Cassandra Collection

This is the screenshot in which data are entered in the map collection.

Cassandra Collection
insert into Firm.Dept(id,Deptprojects) values(5,{'IT': 'Diabestes On Data Science', 'BD': 'Consumer cost to sales effect'});

 

Keep the following collection restrictions in mind:

  • Never put more than 2 billion items in a collection since you can only query up to that amount.
  • A map collection can have up to 65,535 keys at a time.
  • A list or a map collection item can only be 2GB in size.
  • A set collection item can be 65,535 bytes in size at most.
  • To avoid delays when querying, keep collections small.
  • Cassandra can't "slice" collections; she reads them all at once, which has an influence on performance. As a result, collections should be considerably less than the mentioned maximums. Internally, the collection is unpaged.
  • Some insertions in lists will result in a read-before-write action. The use of sets is recommended over the use of individual pieces.