DataStax Python driver
To use the DataStax 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
-
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).
-
Download and install a supported Python version. Python 3.4, 3.5, 3.6, 3.7, and 3.8 are supported, and CPython (the standard Python implementation) and PyPy are supported and tested.
Install the Python driver
-
Install the DataStax Python driver:
pip install cassandra-driver
Make sure you use a driver version that is compatible with Astra DB. For more information, see DataStax driver matrix.
-
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 Developing applications with DataStax drivers.