Friday 21 February 2020

Cassandra: Time related data types

Below table summarizes time related data types.

Data Type
Description
timestamp
It represent date + time. It is number of milliseconds since the epoch.

It can be specified as milliseconds since the epoch or, it can be represented as string in ISO 8601 format.

Example of ISO 8601 formats
yyyy-mm-ddThh:mm:ss.ffffff
yyyy-mm-dd
yyyy-mm-ddThh:mm:ss.nnnnnn+|-hh:mm
timeuuid
Type 1 UUID. This is generally used as a “conflict-free” timestamp.
time
A time with nanosecond precision

Example
CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT PRIMARY KEY, 
  first_name VARCHAR,
  description BLOB,
  male BOOLEAN,
  salary DOUBLE,
  ip_address inet,
  date_of_birth date,
  joining_time timestamp,
  unique_id uuid
);

INSERT INTO cassandratutorial.employee (id, first_name, description, male, salary, ip_address, date_of_birth, joining_time, unique_id) VALUES (1, 'Krishna', textAsBlob('I am Krishna, I am intrested in blogging, trekking'), true, 12345.67, '192.168.2.3', '1985-05-24', 1555494268, uuid());

cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
   ... { 
   ...   'class' : 'SimpleStrategy', 
   ...   'replication_factor' : 1 
   ... };
cqlsh> 
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
   ...   id INT PRIMARY KEY, 
   ...   first_name VARCHAR,
   ...   description BLOB,
   ...   male BOOLEAN,
   ...   salary DOUBLE,
   ...   ip_address inet,
   ...   date_of_birth date,
   ...   joining_time timestamp,
   ...   unique_id uuid
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee (id, first_name, description, male, salary, ip_address, date_of_birth, joining_time, unique_id) VALUES (1, 'Krishna', textAsBlob('I am Krishna, I am intrested in blogging, trekking'), true, 12345.67, '192.168.2.3', '1985-05-24', 1555494268, uuid());
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | date_of_birth | description                                                                                            | first_name | ip_address  | joining_time                    | male | salary   | unique_id
----+---------------+--------------------------------------------------------------------------------------------------------+------------+-------------+---------------------------------+------+----------+--------------------------------------
  1 |    1985-05-24 | 0x4920616d204b726973686e612c204920616d20696e7472657374656420696e20626c6f6767696e672c207472656b6b696e67 |    Krishna | 192.168.2.3 | 1970-01-19 00:04:54.268000+0000 | True | 12345.67 | bba0d1c0-b4c1-4e14-b36e-8ac2d2519bd3

(1 rows)
cqlsh>

Previous                                                    Next                                                    Home

No comments:

Post a Comment