How to insert timestamp data in Cassandra from C program?

359 views
Skip to first unread message

Shalini Bani

unread,
Sep 6, 2019, 2:48:55 PM9/6/19
to DataStax C++ Driver for Apache Cassandra User Mailing List

I have the following code for a simple query of insertion into Cassandra. I am trying to use prepared statement here as the regular statement is not able to insert the timestamp in timestamp column of table time_demo.

      CassFuture* connect_future = NULL;
      CassCluster* cluster = cass_cluster_new();
      CassSession* session = cass_session_new();
      char* hosts = "127.0.0.1";

      time_t rt= time(NULL);
      struct tm * timeinfo;

      timeinfo = localtime ( &rt );
      char lt[20];
      strftime(lt, sizeof(lt), "%Y-%m-%d %T", timeinfo);

      /* Add contact points */
      cass_cluster_set_contact_points(cluster, hosts);

      /* Provide the cluster object as configuration to connect the session  with a specified keyspace*/
      connect_future = cass_session_connect_keyspace(session, cluster,"test_keyspace");

     
       if(cass_future_error_code(connect_future)== CASS_OK)
{
CassFuture* prepare_future
= cass_session_prepare(session, "INSERT INTO time_demo(id,time) VALUES(now(),?);");
if (cass_future_error_code(prepare_future) == CASS_OK)
{

/* Get the prepared object from the future */
const CassPrepared* prepared = cass_future_get_prepared(prepare_future);

/* The future can be freed immediately after getting the prepared object */
cass_future_free(prepare_future);

/* The prepared object can now be used to create statements that can be executed */
CassStatement* statement = cass_prepared_bind(prepared);

/* Bind variables by name this time (this can only be done with prepared statements)*/
cass_statement_bind_string_by_name(statement, "time", lt);


CassFuture* result_future = cass_session_execute(session, statement);
cass_prepared_free(prepared);

CassError rc = cass_future_error_code(result_future);
printf("Query result %s ",cass_error_desc(rc));

}

cass_cluster_free(cluster);
cass_session_free(session);
}
}

The above code is not able to insert timestamp into time_demo table. It is only inserting NULL instead. Can someone please tell the correct way of inserting the timestamp?
Also, is it only possible to insert timestamp using prepared statement but not regular statement because I have tried inserting timestamp
with regular statement but in that case nothing was getting inserted. The table was completely blank.

I am using C driver for Cassandra version 2.13.

Michael Penick

unread,
Sep 6, 2019, 3:29:55 PM9/6/19
to cpp-dri...@lists.datastax.com
From the protocol docs `timestamp` is:

> 6.17 timestamp
>
> An 8 byte two's complement integer representing a millisecond-precision
> offset from the unix epoch (00:00:00, January 1st, 1970). Negative values
> represent a negative offset from the epoch.

The `time()` function returns the offset in seconds since 1970-01-01 00:00:00 +0000 (UTC). So you can use `cass_statement_bind_int64_by_name()` and convert the result of `time()` to milliseconds by multiplying by 1000:

cass_statement_bind_int64_by_name(statement, "time", time(NULL) * 1000);

If you need millisecond precision from your local clock you might use `gettimeofday()` instead.

Mike

--
To unsubscribe from this group and stop receiving emails from it, send an email to cpp-driver-us...@lists.datastax.com.
Message has been deleted

Shalini Bani

unread,
Sep 6, 2019, 5:02:10 PM9/6/19
to DataStax C++ Driver for Apache Cassandra User Mailing List
Thanks. I thought that if I insert it as a string then it should work just like how it works when working from cqlsh. Seems that it doesn't.
Reply all
Reply to author
Forward
0 new messages