Friday 21 February 2020

Cassandra: How to get current timestamp in cql?

You can use now() and ‘toTimestamp’ function to get the current time stamp in cql.

Example
INSERT INTO cassandratutorial.employee(id, first_name, joining_time) VALUES (1, 'Krishna', toTimestamp(now()));

Complete cql script
CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
{ 
  'class' : 'SimpleStrategy', 
  'replication_factor' : 1 
};

CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT PRIMARY KEY, 
  first_name VARCHAR,
  joining_time timestamp
);

INSERT INTO cassandratutorial.employee(id, first_name, joining_time) VALUES (1, 'Krishna', toTimestamp(now()));

Cqlsh console output

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,
   ...   joining_time timestamp
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee(id, first_name, joining_time) VALUES (1, 'Krishna', toTimestamp(now()));
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | first_name | joining_time
----+------------+---------------------------------
  1 |    Krishna | 2019-04-20 07:26:48.529000+0000

(1 rows)




Previous                                                    Next                                                    Home

1 comment: