Deep dive into Cassandra Query Language Collections and user defined data types.

Last updated on May 30 2022
Lalit Kolgaonkar

Table of Contents

Deep dive into Cassandra Query Language Collections and user defined data types.

Cassandra – CQL Datatypes

CQL provides a rich set of built-in data types, including collection types. Along with these data types, users can also create their own custom data types. The following table provides a list of built-in data types available in CQL.

Data Type Constants Description
ascii strings Represents ASCII character string
bigint bigint Represents 64-bit signed long
blob blobs Represents arbitrary bytes
Boolean booleans Represents true or false
counter integers Represents counter column
decimal integers, floats Represents variable-precision decimal
double integers Represents 64-bit IEEE-754 floating point
float integers, floats Represents 32-bit IEEE-754 floating point
inet strings Represents an IP address, IPv4 or IPv6
int integers Represents 32-bit signed int
text strings Represents UTF8 encoded string
timestamp integers, strings Represents a timestamp
timeuuid uuids Represents type 1 UUID
uuid uuids Represents type 1 or type 4
UUID
varchar strings Represents uTF8 encoded string
varint integers Represents arbitrary-precision integer

Collection Types

Cassandra Query Language also provides a collection data types. The following table provides a list of Collections available in CQL.

Collection Description
list A list is a collection of one or more ordered elements.
map A map is a collection of key-value pairs.
set A set is a collection of one or more elements.

User-defined datatypes

Cqlsh provides users a facility of creating their own data types. Given below are the commands used while dealing with user defined datatypes.

  • CREATE TYPE − Creates a user-defined datatype.
  • ALTER TYPE − Modifies a user-defined datatype.
  • DROP TYPE − Drops a user-defined datatype.
  • DESCRIBE TYPE − Describes a user-defined datatype.
  • DESCRIBE TYPES − Describes user-defined datatypes.

Cassandra – CQL Collections

CQL provides the facility of using Collection data types. Using these Collection types, you can store multiple values in a single variable. This blog explains how to use Collections in Cassandra.

List

List is used in the cases where

  • the order of the elements is to be maintained, and
  • a value is to be stored multiple times.

You can get the values of a list data type using the index of the elements in the list.

Creating a Table with List

Given below is an example to create a sample table with two columns, name and email. To store multiple emails, we are using list.

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

Inserting Data into a List

While inserting data into the elements in a list, enter all the values separated by comma within square braces [ ] as shown below.

cqlsh:tecklearn> INSERT INTO data(name, email) VALUES (‘ramu’,

[‘abc@gmail.com’,’cba@yahoo.com’])

Updating a List

Given below is an example to update the list data type in a table called data. Here we are adding another email to the list.

cqlsh:tecklearn> UPDATE data

… SET email = email +[‘xyz@tecklearn.com’]

… where name = ‘ramu’;

Verification

If you verify the table using SELECT statement, you will get the following result −

cqlsh:tecklearn> SELECT * FROM data;

 

name | email

——+————————————————————–

ramu | [‘abc@gmail.com’, ‘cba@yahoo.com’, ‘xyz@tecklearn.com’]

 

(1 rows)

SET

Set is a data type that is used to store a group of elements. The elements of a set will be returned in a sorted order.

Creating a Table with Set

The following example creates a sample table with two columns, name and phone. For storing multiple phone numbers, we are using set.

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

Inserting Data into a Set

While inserting data into the elements in a set, enter all the values separated by comma within curly braces { } as shown below.

cqlsh:tecklearn> INSERT INTO data2(name, phone)VALUES (‘rahman’,    {9848022338,9848022339});

Updating a Set

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

cqlsh:tecklearn> UPDATE data2

… SET phone = phone + {9848022330}

… where name = ‘rahman’;

Verification

If you verify the table using SELECT statement, you will get the following result −

cqlsh:tecklearn> SELECT * FROM data2;

 

name | phone

——–+————————————–

rahman | {9848022330, 9848022338, 9848022339}

 

(1 rows)

MAP

Map is a data type that is used to store a key-value pair of elements.

Creating a Table with Map

The following example shows how to create a sample table with two columns, name and address. For storing multiple address values, we are using map.

cqlsh:tecklearn> CREATE TABLE data3 (name text PRIMARY KEY, address

map<timestamp, text>);

Inserting Data into a Map

While inserting data into the elements in a map, enter all the key : value pairs separated by comma within curly braces { } as shown below.

cqlsh:tecklearn> INSERT INTO data3 (name, address)

VALUES (‘robin’, {‘home’ : ‘hyderabad’ , ‘office’ : ‘Delhi’ } );

Updating a Set

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

cqlsh:tecklearn> UPDATE data3

… SET address = address+{‘office’:’mumbai’}

… WHERE name = ‘robin’;

Verification

If you verify the table using SELECT statement, you will get the following result −

cqlsh:tecklearn> select * from data3;

name | address

——-+——————————————-

robin | {‘home’: ‘hyderabad’, ‘office’: ‘mumbai’}

 

(1 rows)

Cassandra – CQL User Defined Datatypes

CQL provides the facility of creating and using user-defined data types. You can create a data type to handle multiple fields. This blog explains how to create, alter, and delete a user-defined data type.

Creating a User-defined Data Type

The command CREATE TYPE is used to create a user-defined data type. Its syntax is as follows −

CREATE TYPE <keyspace name>. <data typename>

( variable1, variable2).

Example

Given below is an example for creating a user-defined data type. In this example, we are creating a card_details data type containing the following details.

Field Field name Data type
credit card no num int
credit card pin pin int
name on credit card name text
cvv cvv int
Contact details of card holder phone set

cqlsh:tecklearn> CREATE TYPE card_details (

… num int,

… pin int,

… name text,

… cvv int,

… phone set<int>

… );

Note − The name used for user-defined data type should not coincide with reserved type names.

Verification

Use the DESCRIBE command to verify whether the type created has been created or not.

CREATE TYPE tecklearn.card_details (

num int,

pin int,

name text,

cvv int,

phone set<int>

);

Altering a User-defined Data Type

ALTER TYPE − command is used to alter an existing data type. Using ALTER, you can add a new field or rename an existing field.

Adding a Field to a Type

Use the following syntax to add a new field to an existing user-defined data type.

ALTER TYPE typename

ADD field_name field_type;

The following code adds a new field to the Card_details data type. Here we are adding a new field called email.

cqlsh:tecklearn> ALTER TYPE card_details ADD email text;

Verification

Use the DESCRIBE command to verify whether the new field is added or not.

cqlsh:tecklearn> describe type card_details;

CREATE TYPE tecklearn.card_details (

num int,

pin int,

name text,

cvv int,

phone set<int>,

);

Renaming a Field in a Type

Use the following syntax to rename an existing user-defined data type.

ALTER TYPE typename

RENAME existing_name TO new_name;

The following code changes the name of the field in a type. Here we are renaming the field email to mail.

cqlsh:tecklearn> ALTER TYPE card_details RENAME email TO mail;

Verification

Use the DESCRIBE command to verify whether the type name changed or not.

cqlsh:tecklearn> describe type card_details;

CREATE TYPE tecklearn.card_details (

num int,

pin int,

name text,

cvv int,

phone set<int>,

mail text

);

Deleting a User-defined Data Type

DROP TYPE is the command used to delete a user-defined data type. Given below is an example to delete a user-defined data type.

Example

Before deleting, verify the list of all user-defined data types using DESCRIBE_TYPES command as shown below.

cqlsh:tecklearn> DESCRIBE TYPES;

card_details card

From the two types, delete the type named card as shown below.

cqlsh:tecklearn> drop type card;

Use the DESCRIBE command to verify whether the data type dropped or not.

cqlsh:tecklearn> describe types;

 

card_details

So, this brings us to the end of blog. This Tecklearn ‘Deep dive into Cassandra Query Language Collections and user defined data types’ helps you with commonly asked questions if you are looking out for a job in Cassandra and No-SQL Database Domain.

If you wish to learn HBase and build a career in Cassandra or No-SQL Database domain, then check out our interactive, Apache Cassandra Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/apache-cassandra-training/

Apache Cassandra Training

About the Course

Take your career to the next level as a certified Apache Cassandra developer by acquiring all the skills through our hands-on training sessions. Tecklearn’s Apache Cassandra Certification Training is designed by professionals as per the industry requirements and demands. This Cassandra Certification Training helps you to master the concepts of Apache Cassandra including Cassandra Architecture, its features, Cassandra Data Model, and its Administration. Our Cassandra certification training course lets you master the high availability NoSQL distributed database.

Why Should you take Apache Cassandra Training?

  • The average salary of a Software Engineer with Apache Cassandra skill is $120,500 per year. – Payscale.com
  • Cassandra is in use at Constant Contact, CERN, Comcast, eBay, GitHub, GoDaddy, Hulu, Instagram, Intuit, Netflix, Reddit, The Weather Channel, and over 1500 more companies that have large, active data sets.
  • Apache Cassandra is one of the most widely used NoSQL database. It offers features such as Fault Tolerance, Scalability, Flexible Data Storage and its efficient writes, which makes it the perfect database for various purposes.

What you will Learn in this Course?

Introduction to Big Data, and Cassandra

  • What is Big Data
  • Limitations of RDBMS
  • NoSQL and it’s Characteristics
  • CAP Theorem
  • Basic concepts of Cassandra
  • Features of Cassandra

Cassandra Data model, Installation and setup

  • Installation of Cassandra
  • Key concepts and deployment of non-relational database, column-oriented database, Data Model – column, column family

Cassandra Architecture

  • Explain the Architecture of Cassandra
  • Different Layers of Cassandra Architecture
  • Partitioning and Snitches
  • Explain Vnodes and How Read and Write Path works
  • Understand Compaction, Anti-Entropy and Tombstone
  • Describe Repairs in Cassandra

Deep Dive into Cassandra Database

  • Describe Different Data Types Used in Cassandra
  • Explain Collection Types
  • Describe What are CRUD Operations
  • Implement Insert, Select, Update and D        elete of various elements
  • Implement Various Functions Used in Cassandra
  • Describe Importance of Roles and Indexing

Backup & Restore and Performance Tuning

  • Learn backup and restore functionality and its importance
  • Create a snapshot using Nodetool utility
  • Restore a snapshot
  • Understand how to choose the right balance of the following resources: memory, CPU, disks, number of nodes, and network.
  • Understand all the logs created by Cassandra
  • Explain the purpose of different log files
  • Configure the log files
  • Learn about Performance Tuning
  • Integration with Spark and Kafka

Advance Modelling

  • Rules of Cassandra data modelling
  • Modelling data around queries
  • Creating table for data queries

Deploying the IDE for Cassandra applications

  • Learning key drivers
  • Deploying the IDE for Cassandra applications and cluster connection
  • Data query implementation

Cassandra Administration

  • Understanding Node Tool Utility
  • Cluster management using Command Line Interface
  • Management and Monitoring using DataStax Ops Center

Cassandra API and Summarization

  • Cassandra client connectivity
  • Connection pool internals
  • Cassandra API
  • Features and concepts of Hector client
  • Thrift, JAVA code and Summarization

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Deep dive into Cassandra Query Language Collections and user defined data types."

Leave a Message

Your email address will not be published. Required fields are marked *