A Beginner
A Beginner

Reputation: 389

Not able to connect to a keyspace using C driver for Cassandra

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");

      //After this line program exits
      CassFuture* prepare_future
          = cass_session_prepare(session, "INSERT INTO time_demo(id,time) VALUES(now(),?);");

After the last line my program ends abruptly. I wanted to connect to a keyspace test_keyspace while also using a prepared statement. I am guessing the program is getting terminated because of that because I haven't written the code properly for it.

Can anyone please point out the mistake that I have made here? I am using Cassandra 2.13 driver for C.

Upvotes: 0

Views: 171

Answers (1)

Alex Ott
Alex Ott

Reputation: 87119

You need to wait until the connection etablished - the simplest way to do is to use something like:

CassError rc = cass_future_error_code(connect_future);

after you got OK from driver, you can start to prepare queries, but you also need to wait for result of prepare, maybe with cass_future_error_code as well, or some other way.

Otherwise, you fire 2 async operations and don't wait for results, and your program finishes - C/C++ driver is asynchronous by design. Please refer to Getting Started document that describes how to use it correctly.

Upvotes: 1

Related Questions