question

shalini.bani_178035 avatar image
shalini.bani_178035 asked shalini.bani_178035 commented

How to insert timestamp data in Cassandra from C program?

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.

drivertimestampc
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

michael.penick_64230 avatar image
michael.penick_64230 answered shalini.bani_178035 commented

From the protocol docs the CQL `timestamp` type 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.

1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

shalini.bani_178035 avatar image shalini.bani_178035 commented ·

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.

1 Like 1 ·