Cassandra – Data Types
In this tutorial, we will learn about the Data Types in Cassandra CQL language. DataTypes generally define the type of data a column can hold along with their own significance. CQL language supports the below list Data types:
1. Native Types
2. Collection Types
3. User-defined Types
4. tuple types
TYPE | CONSTANTS | DESCRIPTION |
ascii | string | ASCII character string |
bigint | integer | 64-bit signed long |
blob | blob | Arbitrary bytes |
boolean | boolean | Either true or false |
counter | integer | Counter column |
date | integer, string | A date (with no corresponding time value). |
decimal | integer, float | Variable-precision decimal |
double | integer float | 64-bit IEEE-754 floating point |
float | integer, float | 32-bit IEEE-754 floating point |
inet | string | An IP address |
int | integer | 32-bit signed int |
smallint | integer | 16-bit signed int |
text | string | UTF8 encoded string |
time | integer, string | A time with nanosecond precision |
timestamp | integer, string | A timestamp with millisecond precision. |
timeuuid | uuid | Version 1 UUID |
tinyint | integer | 8-bit signed int |
uuid | uuid | A UUID (of any version) |
varchar | string | UTF8 encoded string |
varint | integer | Arbitrary-precision integer |
Collection Types
There are 3 types of collection datatypes :
1. Maps
Map is a set of Key-value pairs.Where keys are unique.
Example:
CREATE TABLE Blog ( id int PRIMARY KEY, data map <text, text> );
2. Sets
Set is collection of unique values.
Example:
CREATE TABLE Blog ( ID int PRIMARY KEY, tags set<text> );
3. Lists
List is a collection of non-unique values which can be ordered with their position.
Example:
CREATE TABLE plays ( id text PRIMARY KEY, Name text, posts list<int> )
User Defined Type
UDT can be created, modified and removed using below commands.
– CREATE TYPE
– ALTER TYPE
– DROP TYPE
Example:
CREATE TYPE Blogger ( city text, Name text, Phone int ) CREATE TYPE Blogs ( ID int, Blogname text, Address map<text, Blogger> )
Tuples
Tuples are a set of different types of elements.
Example:
CREATE TABLE Blogs ( BlogId text, BlogType tuple<int, text> )