Connect with the Python driver
Because Astra DB is based on Apache Cassandra®, you can use Cassandra drivers to connect to your Astra DB Classic databases.
To use the Python driver, you need to install the driver and its dependencies, and then connect the driver to your Astra DB Classic database. Once connected, you can write scripts that use the driver to run commands against your database.
Prerequisites
-
Install Python 3.7 or later.
Python 3.4, 3.5, and 3.6 can be used if necessary for legacy applications that require an earlier version of the Python driver.
-
Download your database’s Secure Connect Bundle (SCB).
For multi-region databases, download the Secure Connect Bundle (SCB) for a region that is geographically close to your application to reduce latency.
If you need to connect to multiple regions in the same application, you need the Secure Connect Bundle (SCB) for each region, and your driver code must instantiate one root object (
session
) for each region. For more information, see Best practices for Cassandra drivers. -
Set the following environment variables:
-
ASTRA_DB_ID
: The database ID. -
ASTRA_DB_KEYSPACE
: A keyspace in your database, such asdefault_keyspace
. -
ASTRA_DB_APPLICATION_TOKEN
: An application token with the Database Administrator role.
-
Driver authentication methods
There are two driver authentication methods: token
authentication, or clientId
and secret
authentication.
-
Token authentication
-
Client ID and secret authentication
This authentication method is supported and recommended for most recent driver versions.
In your driver authentication code, pass the literal string token
as the username and your application token value (AstraCS:…
) as the password.
For example:
("token", "AstraCS:...")
If you are on an older driver version that doesn’t support token
authentication, then you might need to use clientId
and secret
.
When you generate an application token, download or copy the token.json
that contains the following values:
{
"clientId": "CLIENT_ID",
"secret": "CLIENT_SECRET",
"token": "APPLICATION_TOKEN"
}
In your driver authentication code, pass clientId
as the username and secret
as the password.
For example:
("CLIENT_ID", "SECRET")
For more information, see Token details.
Install the Python driver
-
pip install cassandra-driver
If you choose to install an earlier version, make sure you choose a version that is compatible with Astra DB. For more information, see Cassandra drivers supported by DataStax.
-
Verify the installation:
pip show cassandra-driver
Make sure the returned
Version
is the latest version or the specific version that you installed.
Connect the Python driver
-
In the root of your Python project, create a
connect_database.py
file:cd python_project touch connect_database.py
-
Copy the following connection code into
connect_database.py
, and then replacePATH_TO_SCB
with the absolute path to your database’s Secure Connect Bundle (SCB) (secure-connect-DATABASE_NAME.zip
):connect_database.pyimport os from cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider import json with open(ASTRA_TOKEN_PATH, "r") as f: creds = json.load(f) ASTRA_DB_APPLICATION_TOKEN = creds["token"] cluster = Cluster( cloud={ "secure_connect_bundle": PATH_TO_SCB, }, auth_provider=PlainTextAuthProvider( "token", ASTRA_DB_APPLICATION_TOKEN, ), ) session = cluster.connect()
This code creates a
Cluster
instance to connect to your Astra DB database. You typically have one instance of Cluster for each Astra DB database that you want to interact with. -
Add code that runs a CQL query and prints the output to the console:
connect_database.pyrow = session.execute("select release_version from system.local").one() if row: print(row[0]) else: print("An error occurred.")
-
Save and run your Python script:
python ./connect_database.py
The output prints the
release_version
value from thesystem.local
table in your Astra DB database. -
Extend or modify this script to run other commands against your database or connect to other databases. For more information, see Python driver documentation and DataStax-compatible Cassandra drivers.