DataStax Astra Developer Hub

Welcome to the DataStax Astra Developer Hub. You'll find comprehensive guides and documentation to help you start working with DataStax Astra as quickly as possible. Use the included APIs to create, modify, and terminate databases, and interact with the databases you create. Let's do it!

Docs    API Reference

Using the DataStax Python driver to connect to your database

Use the unified DataStax Python driver to connect to your DataStax Astra database and begin building your own application.

📘

Tip

DataStax recommends using the unified DataStax Python driver. If you have an existing Apache Cassandra or DataStax Enterprise (DSE) Python driver, migrate the driver to a version that is capable of connecting to Astra databases.

Prerequisites

  1. Download and install a supported Python version. Python 2.7, 3.4, 3.5 and 3.6 are supported.

📘

Note

CPython (the standard Python implementation) and PyPy are supported and tested.

  1. Download the secure connect bundle to obtain connection credentials for your DataStax Astra database.

📘

Tip

Alternatively, have a teammate provide access to their Astra database.

Procedure

  1. Install the DataStax Python driver.
pip install cassandra-driver

See the Python driver installation instructions for more information.

  1. Verify that the DataStax Python driver installed successfully:
python -c 'import cassandra; print (cassandra.__version__)'

The version number displays in the console output:
3.23.0

  1. Create a connect_database.py file in the main directory for your Python project.
cd python_project
touch connect_database.py
  1. Copy the following connection code into the connect_database.py file.

📘

Note

Set the cloud_config parameter for the Cluster initialization as shown in the following example. The secure_connect_bundle must include the absolute path to your Astra database credentials (secure-connect-database_name.zip).

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

cloud_config= {
        'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
}
auth_provider = PlainTextAuthProvider('username', 'password')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
  1. After the connection code, add the following code to connect-database.py. This code creates a Cluster instance to connect to your Astra database, runs a CQL query, and prints the output to the console. As the name suggests, you will typically have one instance of Cluster for each Cassandra cluster you want to interact with.
row = session.execute("select release_version from system.local").one()
if row:
    print(row[0])
else:
    print("An error occurred.")
  1. Save and close the connect_database.py file.
  2. Run connect_database.py.
python ./connect_database.py

The console output displays the release_version from the system.local table:

4.0.0.670

Updated 18 days ago



Using the DataStax Python driver to connect to your database


Use the unified DataStax Python driver to connect to your DataStax Astra database and begin building your own application.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.