Connect with the C++ driver
This driver doesn’t support the vector data type. |
DataStax recommends the Data API and clients for Serverless (Vector) databases. You can use the Data API to perform CQL operations on your table data in Serverless (Vector) databases. DataStax recommends drivers only for Serverless (Non-Vector) databases, legacy applications that rely on a driver, or for CQL functions that aren’t supported by the Data API. For more information, see Compare connection methods. |
To use the DataStax C++ driver, you need to install the driver and its dependencies, and then connect the driver to your Astra DB Serverless database with a CassStatement
object.
Once connected, you can write scripts that use the driver to run commands against your database.
Prerequisites
-
Set the following environment variables:
-
ASTRA_DB_ID
: The database ID -
ASTRA_DB_REGION
: A region where your database is deployed and where you want to connect to the database, such asus-east-2
-
ASTRA_DB_KEYSPACE
: A keyspace in your database, such asdefault_keyspace
-
ASTRA_DB_APPLICATION_TOKEN
: An application token with the Database Administrator role.The
token.json
has the following format:{ "clientId": "CLIENT_ID", "secret": "CLIENT_SECRET", "token": "APPLICATION_TOKEN" }
For driver authentication, you can use either
clientId
andsecret
or the literal stringtoken
and theAstraCS
token value. If you are on an older driver version that doesn’t support thetoken
option, then you might need to useclientId
andsecret
. For more information, see Token details.
-
-
Download your database’s Secure Connect Bundle (SCB).
Install the C++ driver
-
Download and install the DataStax C++ driver.
Make sure you use a driver version that is compatible with Astra DB. For more information, see DataStax driver matrix.
Connect the C++ driver
-
In the root of your C++ project, create a
connect_database.c
file:cd my_project touch connect_database.c
-
Copy the following connection code into the
connect_database.c
file, and then replacePATH_TO_SCB
with the absolute path to your database’s Secure Connect Bundle (SCB) (secure-connect-DATABASE_NAME.zip
):connect_database.c#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(); // Set up driver to connect to the cloud using the SCB const char* secure_connect_bundle = "PATH_TO_SCB"; if (cass_cluster_set_cloud_secure_connection_bundle(cluster, secure_connect_bundle) != CASS_OK) { fprintf(stderr, "Unable to configure cloud using the SCB: %s\n", secure_connect_bundle); return 1; } // Set credentials provided when creating your database cass_cluster_set_credentials(cluster, "token", "ASTRA_DB_APPLICATION_TOKEN"); 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; }
This code creates a
CassStatement
object to connect to your Astra DB database.Don’t use the
cass_cluster_set_contact_points()
andcass_cluster_set_ssl()
methods in conjunction with thecass_cluster_set_cloud_secure_connection_bundle()
method. -
Add code to your script that queries your database and prints the output to the console:
connect_database.c/* 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);
-
Build and link your application against the DataStax C++ driver.
-
Linux or macOS
-
Windows
For static linking, use
cassandra_static.a
.cc connect_database.c -I/path/to/cassandra.h -L/path/to/cassandra.so -lcassandra
Include these libraries in your Microsoft Visual Studio project by adding them to the project’s properties under
Configuration Properties/Linker/Input/Additional Dependencies
:-
Link your application against
cassandra.lib
. For static linking, usecassandra_static.lib
. -
cassandra.dll
must be in your application’s runtime path.
-
-
Run the compiled application.
The console output includes the
release_version
value from thesystem.local
table. -
Extend or modify this script to run other commands against your database or connect to other databases. For more information, see C++ driver documentation and Developing applications with DataStax drivers.