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.

DataStax recommends using the unified DataStax driver. See this blog post for more information. You can also use the DataStax Enterprise (DSE) drivers, which exposes the same API for connecting to Cassandra databases.

Prerequisites

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

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

  • 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 connect to your Astra DB database using the drivers, download the secure database bundle from the DataStax Astra DB console that contains the connection credentials.

  1. Open a browser, navigate to Astra DB, and log in.

  1. From your Dashboard page, select your database.

  1. On the Overview page, select Connect.

  1. In the Connect using a driver section, click your preferred language from the list to load language-specific instructions.

    If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.

Note, though, that the bundle URL is the same for all languages.

  1. Click Download Bundle.

    If you have multiple regions, you will have the option to download a bundle for each region from the expanded Download Bundle drop-down menu.

If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. The secure-connect-database_name.zip file downloads, which contains the security certificates and credentials for your database.

VPC peering is only available on Classic databases.

Alternatively, you can right-click the Download credentials link, copy the link source, and then use a curl command to download the secure-connect-database_name.zip file:

curl -L "<secure-connect-database_name-download-URL>" > database_name.zip

Sharing secure connect bundle

Although teammates can access your Astra DB database, it will not display in their list of available databases under My Databases in the Astra DB console.

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.

Connecting Python driver

  1. Install the DataStax Python driver.

    pip install cassandra-driver

    See the Python driver installation instructions for more information.

  2. 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

  3. Create a connect_database.py file in the main directory for your Python project.

    cd python_project
    touch connect_database.py
  4. Copy the following connection code into the connect_database.py file.

    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 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()
  5. After the connection code, add the following code to connect-database.py. This code creates a Cluster 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.")
  6. Save and close the connect_database.py file.

  7. Run connect_database.py.

    python ./connect_database.py

    The console output displays the release_version from the system.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.

  1. Install the DataStax Python driver:

DataStax Python driver for Apache Cassandra

pip install cassandra-driver

DSE Python driver

pip install dse-driver

See the Python driver installation instructions for more information.

  1. Verify that the DataStax Python driver installed successfully:

DataStax Python driver for Apache Cassandra

python -c 'import cassandra;
print cassandra.*version*'

DSE Python driver

python -c 'import dse;
print dse.*version*'

The version number displays in the console output: 3.20.0

  1. 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.

DataStax Python driver for Apache Cassandra

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()

DSE Python driver

from dse.cluster import Cluster
from dse.auth import PlainTextAuthProvider

cloud_config = {
   'secure_connect_bundle': '/python_project/creds/secure-connect-database_name.zip'
   }
   auth_provider = PlainTextAuthProvider(username='clientId', 'clientSecret')
   cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
   session = cluster.connect()
  1. 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.