Connecting to your database with the DataStax C++ driver

Use the DataStax C++ driver to connect to your database created using the DataStax Constellation console.

Use the DataStax C++ driver to connect to your Apollo database and begin building your own application.
Tip: DataStax recommends using the DataStax C++ driver for Apache Cassandra. You can also use the DataStax Enterprise (DSE) C++ driver, which exposes the same API for connecting to Cassandra databases.

Prerequisites

  1. Create a DataStax Apollo database.
  2. Download the secure connect bundle to obtain connection credentials for your database.
  3. Download the DataStax C++ driver and dependency packages for your platform using the links in the following table.
    Table 1. DataStax C++ driver download links
    Platform Download links
    CentOS 6 Driver | Dependencies
    CentOS 7 Driver | Dependencies
    Ubuntu 14.04 Driver | Dependencies
    Ubuntu 16.04 Driver | Dependencies
    Ubuntu 18.04 Driver | Dependencies
    Windows Driver | Dependencies
    Other platforms should build the driver from the source code packages.
  4. Using the packages you downloaded, follow the installation instructions for your platform to install the DataStax C++ driver.

Procedure

  1. Create a connect_database.c file in the main directory for your C++ project.
    cd my_project
    touch connect_database.c
  2. Copy the following connection code into the connect_database.c file.
    Note: Both the DataStax C++ driver for Apache Cassandra and the DataStax Enterprise (DSE) C++ driver use the same code to connect and query your DataStax Apollo database. When using the DSE C++ driver, use the header #include <dse.h>.

    The secure_connect_bundle must include the absolute path to your Apollo database credentials (secure-connect-database_name.zip).

    #include <cassandra.h>
    /* Use "#include <dse.h>" when using the DSE C++ driver */
    #include <stdio.h>
    
    int main(int argc, char* argv[]) {
      /* Setup and connect to cluster */
      CassCluster* cluster = cass_cluster_new();
      CassSession* session = cass_session_new();
    
      /* Setup driver to connect to the cloud using the secure connection bundle */
      const char* secure_connect_bundle = "/path/to/secure-connect-database_name.zip";
      if (cass_cluster_set_cloud_secure_connection_bundle(cluster, secure_connect_bundle) != CASS_OK) {
        fprintf(stderr, "Unable to configure cloud using the secure connection bundle: %s\n",
                secure_connect_bundle);
        return 1;
      }
    
      /* Set credentials provided when creating your database */
      cass_cluster_set_credentials(cluster, "username", "password");
    
      CassFuture* connect_future = cass_session_connect(session, cluster);
    
      if (cass_future_error_code(connect_future) == CASS_OK) {
        /* Use the session to run queries */
      } else {
        /* Handle error */
      }
    
      cass_future_free(connect_future);
      cass_cluster_free(cluster);
      cass_session_free(session);
    
      return 0;
    }
  3. Build and link your application against the DataStax C++ driver.
    • Linux or macOS

      Note: For static linking, use cassandra_static.a or dse_static.a.

      DataStax C++ driver for Apache Cassandra

      cc connect-database.c -I/path/to/cassandra.h -L/path/to/cassandra.so -lcassandra

      DSE C++ driver

      cc connect-database.c -I/path/to/dse.h -L/path/to/dse.so -ldse
    • Windows

      Include these libraries in your Microsoft Visual Studio project by adding them to the project’s properties under Configuration Properties/Linker/Input/Additional Dependencies.

      Note: For static linking, use cassandra_static.lib or dse_static.lib.

      DataStax C++ driver for Apache Cassandra

      Link your application against cassandra.lib. Your application will also require cassandra.dll to be in your runtime path.

      DSE C++ driver

      Link your application against dse.lib. Your application will also require dse.dll to be in your runtime path.

  4. After connecting to your Apollo database, use the following code to query your database.

    This code creates a CassStatement object to connect to your Apollo database, runs a CQL query, and prints the output to the console.

    /* Build statement and execute query */
    const char* query = "SELECT release_version FROM system.local";
    CassStatement* statement = cass_statement_new(query, 0);
    
    CassFuture* result_future = cass_session_execute(session, statement);
    
    if (cass_future_error_code(result_future) == CASS_OK) {
      /* Retrieve result set and get the first row */
      const CassResult* result = cass_future_get_result(result_future);
      const CassRow* row = cass_result_first_row(result);
    
      if (row) {
        const CassValue* value = cass_row_get_column_by_name(row, "release_version");
    
        const char* release_version;
        size_t release_version_length;
        cass_value_get_string(value, &release_version, &release_version_length);
        printf("release_version: '%.*s'\n", (int)release_version_length, release_version);
      }
    
      cass_result_free(result);
    } else {
      /* Handle error */
      const char* message;
      size_t message_length;
      cass_future_error_message(result_future, &message, &message_length);
      fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message);
    }
    
    cass_statement_free(statement);
    cass_future_free(result_future);

What's next

Build your application. See the DataStax C++ driver for Apache Cassandra and DSE C++ driver documentation for more information about using the DataStax C++ driver.