DataStax C++ driver

Before using the DataStax driver, review the Best practices for DataStax drivers to understand the rules and recommendations for improving performance and minimizing resource utilization in applications that use a DataStax driver.

If possible, upgrade to the latest native driver.

For more details about supported software, see the DataStax Support Policy.

Upgrade your driver to a compatible version to connect to DataStax Astra DB databases. For more information, see the DataStax Driver Matrix.

Prerequisites

Working with secure connect bundle

This page explains how to use the secure connect bundle for Astra DB Classic.

If you want to use the secure connect bundle with Astra DB Serverless, see the Astra DB Serverless documentation.

Downloading secure connect bundle

To get the necessary security credentials and certificates for connecting drivers to your Astra DB database, you’ll need to download the secure connect bundle from the DataStax Astra Portal.

  1. Open your Astra Portal and select your database.

  2. On the Overview page, select Connect.

  3. In the Database Essentials section, click Get Bundle.

  4. In the Secure Connect Bundle Download dialog box, use the Select a region dropdown menu to select the region for which you want to download the bundle. If you have multiple regions, you can download a bundle for each region.

    If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. VPC peering is only available on Classic databases.

  5. After selecting a region, various options for downloading the bundle appear. To download the bundle to your local computer, click Download Secure Bundle.

    The secure connect bundle downloads as a ZIP file named secure-connect-<database_name>.zip.

    Expiration of download link

    For added security, the download link for the secure connect bundle expires after five minutes. Once the download link expires, you’ll need to repeat the steps above to regenerate a new download link.

    Note that the secure connect bundle itself never expires.

Sharing secure connect bundle

Although teammates can access your Astra DB database, it doesn’t display in their list of available databases under My Databases in Astra Portal.

After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.

Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database.

For security, delete downloaded connection credentials after sending them to your teammate.

Secure connect bundle contents

The Secure Connect Bundle (SCB) contains the following files:

File Contents

ca.crt

DataStax’s Certificate Authority public certificate

cert

A certificate, unique to the specific SCB

key

A private key, unique to the specific SCB

cert.pfx

A PFX formatted archive containing the certificate and the private key

config.json

A configuration file with information on how to securely connect to the Astra DB instance associated with the SCB

cqlshrc

A CQLSH profile containing CQL shell session settings

identity.jks

A Java keystore file containing the aforementioned cert & key files

trustStore.jks

A Java keystore file containing the aforementioned ca.crt

Connecting C++ driver

  1. Create a connect_database.c file in the main directory for your C++ project.

    cd my_project
    touch connect_database.c
  1. Copy the following connection code into the connect_database.c file. The secure_connect_bundle must include the absolute path to your Astra database credentials (secure-connect-DATABASE_NAME.zip).

    The cass_cluster_set_contact_points() and cass_cluster_set_ssl() methods should not used in conjunction with the cass_cluster_set_cloud_secure_connection_bundle() method.

    #include <cassandra.h>
    #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, "clientID", "clientSecret");
    
         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;
       }
  2. Build and link your application against the DataStax C++ driver.

    • Linux or macOS

      For static linking, use cassandra_static.a.

      cc connect_database.c -I/path/to/cassandra.h -L/path/to/cassandra.so -lcassandra
    • 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.

      For static linking, use cassandra_static.lib.

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

  1. After connecting to your database, use the following code to query your database. This code creates a CassStatement object to connect to your Astra DB 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);

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com