CQL has a large number of built-in data types, including collection kinds. Users can also construct their own unique data types in addition to standard data kinds. The table below lists the built-in data types accessible in CQL. In this lesson, we'll go over some of the Cassandra database's many data kinds. Cassandra data types support a wide range of data kinds, including collections, native types, tuples, and user-defined types. Let's have a look at them all in this blog.

Post Graduate Program In Data Science

The Ultimate Ticket To Top Data Science Job RolesExplore Course
Post Graduate Program In Data Science

What Is CQL?

Cassandra Query Language (CQL) is a query language that is used with theCassandra datatypes. This version of CQL is compatible with Cassandra 3.x. The primary language for connecting with the Cassandra database is the Cassandra Query Language (CQL). The CQL shell, cqlsh, is the simplest fundamental way to interface with Cassandra. You may use cqlsh to construct keyspaces and tables, insert and query tables, and much more.

Cassandra Data types

Data types often denote the type of variables used in a program. Many data types have been examined in different programming languages. For example, int, char, float, and so on. There are several data kinds in the Cassandra query language.

Built-in, collection, and user-defined data types are among the Cassandra datatypes. The user can select any of these based on the program's requirements.

Built-in Type

CQL has a large number of native data types. These data types are predefined, and we may refer to any of them directly.

Data Type

Constant

Usecase

ascii

strings

ASCII character string representation

bigint

bigint

Represents arbitrary bytes 

blob

blobs

Representation as a 64-bit signed long.

boolean

booleans

Representation as True or false 

counter

integers

Represents the Counter column.

date

integer, string

A date (with no corresponding time value). Details can be found on the dates listed below.

decimal

integers, floats

Variable-precision decimal represents

double

integers

64-bit IEEE-754 floating-point representation

float

integers, floats

32-bit IEEE-754 floating-point representation

inet

strings

An IP address, either IPv4 or IPv6, is represented.

int

integers

32-bit signed int representation

text

strings

Represent a string encoded in UTF8

timestamp

integers, strings

A timestamp is represented

timeuuid

uuids

Type 1 UUID is represented

uuid

uuids

Types 1 and 4 are represented

varchar

strings

The uTF8 encoded string is shown

varint

integers

Integer with arbitrary precision

Free Course: Introduction to Data Science

Learn the Fundamentals of Data ScienceEnroll Now
Free Course: Introduction to Data Science

Collection Data types

We may need to store data of the same type without creating additional columns. Multiple values can be stored in a collection data type. CQL gives us three collection types to assist us: lists, sets, and maps. We may, for example, establish a table with a list of textual items, a list of numbers, or a list of other element kinds.

CQL recognizes three types of collections: maps, sets, and lists. The categories of those collections are specified as follows:

collection_type::= MAP '<' cql_type',' cql_type'>'

| SET '<' cql_type '>'

| LIST '<' cql_type'>'

Collection literals can be used to enter their values:

collection_literal::= map_literal | set_literal | list_literal

map_literal::= '\{' [ term ':' term (',' term : term)* ] '}'

set_literal::= '\{' [ term (',' term)* ] '}'

list_literal::= '[' [ term (',' term)* ] ']'

Set

Using the set data type, we can store several unique values. Similarly, items are not saved in order in Java. Let's make a set:

CREATE TABLE collection_types

(

    primaryKey int PRIMARY KEY,

    email set<text>

);

List

The values of this data type are kept in the form of a list. We are unable to adjust the order of the items. After saving the data in the list, each element is assigned an index. Using these indices, we may obtain data.

Lists, unlike sets, may hold duplicate values. Let us add the following to our table:

ALTER TABLE collection_types

    ADD scores list<text>;

Map

We can use Cassandra's map data type to store data in sets of key-value pairs. Keys are one-of-a-kind items. As a result, we can order maps based on their keys.

Let us now add a new column to our table:

ALTER TABLE collection_types

    ADD address map<uuid, text>;

Using Frozen in a Collection

Multiple components are serialized into a single value by a frozen value. Individual fields can be updated in non-frozen kinds. Cassandra datatypes consider the value of a frozen type to be a blob. Overwriting the full value is required.

column_name collection_type<data_type, frozen<column_name>>

Let’s take an example:

CREATE TABLE mykeyspace.users (

  id uuid PRIMARY KEY,

  name frozen <fullname>,

  direct_reports set<frozen <fullname>>,    

  addresses map<text, frozen <address>>     

  score set<frozen <set<int>>>             

);

Other Data types

  1. A boolean value is just a true/false value.
  2. uuid is a Type 4 UUID that is solely based on random digits. UUIDs can be entered using dash-separated hex digit sequences.
  3. A binary big object (blob) is a slang word for an arbitrary array of bytes in computing. CQL blobs are used to hold media or other binary file formats. The blob size limit is 2 GB, however less than 1 MB is preferred.
  4. The inet type represents IPv4 or IPv6 Internet addresses.

The Ultimate Data Science Job Guarantee Program

6 Month Data Science Course With a Job GuaranteeJoin Today
The Ultimate Data Science Job Guarantee Program

User-Defined Datatypes

Cqlsh gives users the option of building their own data types. An example of a user-defined data type is shown below. In this example, we'll build a card details data type with the following information.

Field

Name of the Field

Data Types

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

CREATE TYPE card_details (

... num int,

... pin int,

... name text,

... cvv int,

... phone set<int>

... );

The commands used when working with user-defined data types are listed below.

1. CREATE TYPE: creates a custom datatype.

2. DROP TYPE: is used to remove a user-defined datatype.

Example

Let’s see how to delete the type named card. Use the syntax written below:

drop type card;

3. ALTER TYPE: is used to modify a user-defined datatype.

To add a new field to an existing user-defined data type, use the syntax below.

ALTER TYPE typename

ADD field_name field_type;

The code that follows adds a new field to the Card details data type. We're going to add a new field named email here.

ALTER TYPE card_details ADD email text;

4. DESCRIBE TYPE: Describes a data type that has been specified by the user.

5. DESCRIBE TYPES: Describes datatypes that have been specified by the user.

Enroll in the PG Program in Data Science to learn over a dozen of data science tools and skills, and get exposure to masterclasses by Purdue faculty and IBM experts, exclusive hackathons, Ask Me Anything sessions by IBM.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.