DataStax Python 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.
Prerequisites
-
Create your database and set your environment variables to save time developing on your database. There are four environment variables, three of which are on the Astra dashboard (database id, region, and keyspace), and one that you must create (token). For more, see Create your database.
-
Download and install a supported Python version. Python 3.4, 3.5, 3.6, 3.7, and 3.8 are supported.
-
Client ID and Client Secret by creating your application token for your username and password.
Working with secure connect bundle
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.
-
Open your Astra Portal and select your database.
-
On the Overview page, select Connect.
-
In the Database Essentials section, click Get Bundle.
-
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.
-
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 linkFor 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 |
---|---|
|
DataStax’s Certificate Authority public certificate |
|
A certificate, unique to the specific SCB |
|
A private key, unique to the specific SCB |
|
A PFX formatted archive containing the certificate and the private key |
|
A configuration file with information on how to securely connect to the Astra DB instance associated with the SCB |
|
A CQLSH profile containing CQL shell session settings |
|
A Java keystore file containing the aforementioned cert & key files |
|
A Java keystore file containing the aforementioned ca.crt |
Connecting Python driver
-
Install the DataStax Python driver.
pip install cassandra-driver
See the Python driver installation instructions for more information.
-
Verify that the DataStax Python driver installed successfully:
python -c 'import cassandra; print (cassandra.__version__)'
The version number displays in the console output:
3.24.0
or greater. -
Create a
connect_database.py
file in the main directory for your Python project.cd python_project touch connect_database.py
-
Copy the following connection code into the
connect_database.py
file.Set the
cloud_config
parameter for theCluster
initialization as shown in the following example. Thesecure_connect_bundle
must include the absolute path to your Astra DB 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()
-
After the connection code, add the following code to
connect-database.py
. This code creates aCluster
instance to connect to your Astra DB 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.")
-
Save and close the
connect_database.py
file. -
Run
connect_database.py
.python ./connect_database.py
The console output displays the
release_version
from thesystem.local
table:4.0.0.670
Migrating Python driver
Complete the following procedure to migrate your existing DataStax Python driver to a version that is capable of connecting to DataStax Astra DB databases created using Astra DB.
-
Install the DataStax Python driver for Apache Cassandra:
pip install cassandra-driver
See the Python driver installation instructions for more information.
-
Verify that the DataStax Python driver for Apache Cassandra installed successfully:
python -c 'import cassandra; print cassandra.*version*'
The version number displays in the console output:
3.20.0
-
In your existing DataStax Python driver code, modify the connection code to use the Astra DB API. In the
cloud_config
parameter, include the path to the secure connect bundle for your Astra DB database (secure-connect-database_name.zip
), as shown in the following example.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('clientId', 'clientSecret') cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider) session = cluster.connect()
-
Run your Python script to connect to your Astra DB database:
python ./my_python_driver.py
If successful, the code builds, compiles, and connects to your Cassandra database.