TechnologyDecember 30, 2014

Getting started with the DataStax C/C++ driver

Michael Penick
Michael PenickDataStax
Getting started with the DataStax C/C++ driver
CassCluster* cluster = cass_cluster_new();
/* Contact points can be added as a comma-delimited list */
cass_cluster_set_contact_points("127.0.0.1,127.0.0.2");
/* Or individually */
cass_cluster_set_contact_points("127.0.0.3");
cass_cluster_set_contact_points("127.0.0.4");
/* DNS can also be used */
cass_cluster_set_contact_points("node1.datastax.com,node2.datastax.com");
CassSession* session = cass_session_new();
CassFuture* connect_future = cass_session_connect(session, cluster);
/* This operation will block until the result is ready */
CassError rc = cass_future_error_code(connect_future);
printf("Connect result: %s\n", cass_error_desc(rc));
cass_future_free(connect_future);
cass_session_free(session);
void on_connect(CassFuture* future, void* data) {
  /* This operation will now return immediately */
  CassError rc = cass_future_error_code(future);
  printf("%s\n", cass_error_desc(rc));
}
CassSession* session = cass_session_new();
CassFuture* connect_future = cass_session_connect(session, cluster);
/* Set a callback instead of waiting for the result to be returned */
cass_future_set_callback(on_connect, NULL);
/* The application's reference to the future can be freed immediately */
cass_future_free(connect_future);
/* Run other application logic */
cass_session_free(session);
CassString insert_query = cass_string_init("INSERT INTO example (key, value) VALUES (?, ?);");
/* There are two bind variables in the query string */
CassStatement* statement = cass_statement_new(insert_query, 2);
/* Bind the values using the indices of the bind variables */
cass_statement_bind_string(statement, 0, cass_string_init("abc"));
cass_statement_bind_int32(statement, 1, 123);
CassFuture* query_future = cass_session_execute(session, statement);
/* Statement objects can be freed immediately after being executed */
cass_statement_free(statement);
/* This will block until the query has finished */
CassError rc = cass_future_error_code(query_future);
printf("Query result: %s\n", cass_error_desc(rc));
cass_future_free(query_future);
CassString insert_query = cass_string_init("INSERT INTO example (key, value) VALUES (?, ?);");
/* Prepare the statement on the Cassandra cluster */
CassFuture* prepare_future = cass_session_prepare(session, insert_query);
/* Wait for the statement to prepare and get the result */
CassError rc = cass_future_error_code(prepare_future);
printf("Prepare result: %s\n", cass_error_desc(rc));
if (rc != CASS_OK) {
  /* Handle error */
  cass_future_free(prepare_future);
  return -1;
}
/* Get the prepared object from the future */
const CassPrepared* prepared = cass_future_get_prepared(prepared_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, "key", cass_string_init("abc"));
cass_statement_bind_int32_by_name(statement, "value", 123);
/* Execute statement (same a the non-prepared code) */
/* The prepared object must be freed */
cass_prepared_free(prepared);
CassString query = cass_string_init("SELECT * FROM example (key, value) WHERE key = ?;");
/* There's only a single variable to bind this time */
CassStatement* statement = cass_statement_new(query, 1);
/* Bind the value using the index of the bind variable */
cass_statement_bind_string(statement, 0, cass_string_init("abc"));
CassFuture* query_future = cass_session_execute(session, statement);
/* Statement objects can be freed immediately after being executed */
cass_statement_free(statement);
/* This will also block until the query returns */
const CassResult* result = cass_future_get_result(future);
/* If there was an error then the result won't be available */
if (result == NULL) {
  /* Handle error */
  cass_future_free(query_future);
  return -1;
}
/* The future can be freed immediately after getting the result object */
cass_future_free(query_future);
/* This can be used to retrieve on the first row of the result */
const CassRow* row = cass_result_first_row(result);
/* Now we can retrieve the column values from the row */
CassString key;
/* Get the column value of "key" by name */
cass_value_get_string(cass_row_get_column_by_name(row, "key"), &key);
cass_int32_t value;
/* Get the column value of "value" by name */
cass_value_get_int32(cass_row_get_column_by_name(row, "value"), &value);
/* This will free the future as well as the string pointed to by the CassString 'key' */
cass_result_free(result);
/* Create a new row iterator from the result */
CassIterator* row_iterator = cass_iterator_from_result(result);
while (cass_iterator_next(row_iterator)) {
  const CassRow* row = cass_iterator_get_row(row_iterator);
  /* Copy data from the row */
}
cass_iterator_free(row_iterator);
CassString query = cass_string_init("SELECT * FROM example");
CassStatement* statement = cass_statement_new(query, 0);
/* Return a 100 rows every time this statement is executed */
cass_statement_set_paging_size(statement, 100);
cass_bool_t has_more_pages = cass_true;
while (has_more_pages) {
  CassFuture* query_future = cass_session_execute(session, statement);
  const CassResult* result = cass_future_get_result(future);
  if (result == NULL) {
     /* Handle error */
     cass_future_free(query_future);
     break;
  }
  /* Get values from result... */
  /* Check to see if there are more pages remaining for this result */
  has_more_pages = cass_result_has_more_pages(result);
  if (has_more_pages) {
    /* If there are more pages we need to set the position for the next execute */
    cass_statement_set_paging_state(statement, result);
  }
  cass_result_free(result); 
}
/* This logged batch will makes sure that all the mutations eventually succeed */
CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);
/* Statements can be immediately freed after being added to the batch */
{
  CassStatement* statement = cass_statement_new(cass_string_init("INSERT INTO example1(key, value) VALUES ('a', '1')"), 0);
  cass_batch_add_statement(batch, statement);
  cass_statement_free(statement);
}
{
  CassStatement* statement = cass_statement_new(cass_string_init("UPDATE example2 set value = '2' WHERE key = 'b'"), 0);
  cass_batch_add_statement(batch, statement);
  cass_statement_free(statement);
}
{
  CassStatement* statement = cass_statement_new(cass_string_init("DELETE FROM example3 WHERE key = 'c'"), 0);
  cass_batch_add_statement(batch, statement);
  cass_statement_free(statement);
}
CassFuture* batch_future = cass_session_execute_batch(session, batch);
/* Batch objects can be freed immediately after being executed */
cass_batch_free(batch);
/* This will block until the query has finished */
CassError rc = cass_future_error_code(batch_future);
printf("Batch result: %s\n", cass_error_desc(rc));
cass_future_free(batch_future);
Discover more
Get StartedC++
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.