Cassandra Data Types & Data Expiration

Cassandra Data Types:

Apache Cassandra data types are of different kinds. I will explain all data types one by one that the latest version Cassandra supports. Cassandra Query Language provides rich set of data types including different kind of collection. CQL also provide user the facility to create custom data types in Cassandra along with these pre-defined data types. The table below shows the pre-defined data types and their descriptions.

Data Type Constants Description
Int Integers To store 32 bit signed integer
Bigint Integers Represents 64-bit signed long
Varint Integers Represents Arbitrary precision integer
Float Integers, floats To Store 32-bit floating point number
Double Integers, floats To Store 64-bit floating point number
Decimal Integers, floats Variable precision decimal
Counter Integers Distributed counter values 64 bit
Boolean Booleans Represents True or false value
Varchar Strings UTF-8 encoded string
Text Strings UTF-8 encoded strings
Inet Strings To Store IP address in IPV4 or IPV6 format
ascii Strings US-Ascii character string
Blob Blobs Arbitrary bytes in hexadecimal
Frozen Tuples, collections, user defined types Stores cassandra types
Tuple   A group of 2,3 fields
List   Collection of elements
Set   Collection of elements
Map   Json style collection of
elements
Uuid Uuids Standard uuid
Timeuuid Uuids Type 1 uuid
Timestamp Integers, strings Id generated with date plus
time

Cassandra User-defined data types:

Cassandra Query Language also provides privileges to users to create their own data types based on their data. I have explains some Cqlsh command for dealing with user defined data types.

  • CREATE TYPE – This command is used to create a user-defined DataType.
  • DROP TYPE – Command used to drops a user-defined Data Type.
  • ALTER TYPE – To modify a user-defined Data Type.
  • DESCRIBE TYPE – To Describes a user-defined Data Type.

Collection Types in Cassandra Query Language (CQL):

Cassandra Query Language (CQL) also provides a collection data type to store object like data in Cassandra nosql database. The table below provides a list of Collections available in Cassandra Query Language.

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

Cassandra Automatic Data Expiration

Time to live (TTL) in Cassandra

Apache Cassandra also provides the facility of TTL. A user can set time to live property of each data record. It means that data record is expired after a certain time period. That you have already set while inserting data in Cassandra.

While inserting data on each record you have to set the “ttl” time to live time in seconds. After that much of time Cassandra automatically removes that data record. You can set the time from 1 second to many days.

For example, if you set ttl value 100 seconds while inserting a data record. The Data record will automatically be deleted as 100 seconds passes. When ttl time elapsed, that data record is marked with a tombstone.

A tombstone exists for a grace period. During compaction process data is permanently deleted from database.

Syntax for Cassandra Time to Live (TTL):

Insert into KeyspaceName.TableName(ColumnNames) values(ColumnValues)
using ttl TimeInseconds;

        

Execution of Cassandra TTL

Here is the screenshot of data insertion example where data is being inserted in Student table with ttl value of 150 seconds.

Cassandra data types: TTL property demo
Cassandra data types: TTL property demo

Here is the screenshot which shows data expiration. Data is expired after 150 seconds and data is removed after the time elapsed.

Cassandra data types: Retrieval after data expiration
Cassandra data types: Retrieval after data expiration

See Also :

Cassandra Data Model

Cassandra Architecture

Download and install Cassandra

Cassandra Cluster setup

Summary:

This article briefly explain the Apache Cassandra data types as well as user defined data types in Cassandra nosql database. Cassandra query language commands for user defined data types to create alter and drop as well as describe the user defined data types. Cassandra time to live (TTL) property as well as collection data types in cassandra.