Cassandra TTL & Cassandra CQL Data Types (Example)
Cassandra Data Types
Cassandra supports different types of data types. Here is the table that shows data types, their constants, and description.
CQL Type | Constants | Description |
---|---|---|
ascii | Strings | US-Ascii character string |
Bigint | Integers | 64-bit signed long |
Blob | Blobs | Arbitrary bytes in hexadecimal |
Boolean | Booleans | True or false |
Counter | Integers | Distributed counter values 64 bit |
Decimal | Integers, floats | Variable precision decimal |
Double | Integers, floats | 64-bit floating point |
Float | Integers, floats | 32-bit floating point |
Frozen | Tuples, collections, user defined types | Stores cassandra types |
Inet | Strings | IP address in IPV4 or IPV6 format |
Int | Integers | 32 bit signed integer |
List | Collection of elements | |
Map | Json style collection of elements | |
Set | Collection of elements | |
Text | Strings | UTF-8 encoded strings |
Timestamp | Integers, strings | Id generated with date plus time |
Timeuuid | Uuids | Type 1 uuid |
Tuple | A group of 2,3 fields | |
Uuid | Uuids | Standard uuid |
Varchar | Strings | UTF-8 encoded string |
Varint | Integers | Arbitrary precision integer |
Cassandra TTL (Time to Live) using Automatic Data Expiration
Cassandra provides functionality by which data can be automatically expired.
During data insertion, you have to specify ‘ttl’ value in seconds. ‘ttl’ value is the time to live value for the data. After that particular amount of time, data will be automatically removed.
For example, specify ttl value 100 seconds during insertion. Data will be automatically deleted after 100 seconds. When data is expired, that expired data is marked with a tombstone.
A tombstone exists for a grace period. After data is expired, data is automatically removed after compaction process.
Syntax
Insert into KeyspaceName.TableName(ColumnNames) values(ColumnValues) using ttl TimeInseconds;
Example
Here is the snapshot where data is being inserted in Student table with ttl value of 100 seconds.
insert into University.Student(rollno,name,dept,semester) values(3,'Guru99','CS’,7) using ttl 100;
Here is the snapshot where data is automatically expired after 100 seconds and data is automatically removed.