Migrating your DataStax Python driver to connect with Astra databases

Migrate your existing DataStax Python driver to a version compatible with Cassandra databases.

Complete the following procedure to migrate your existing DataStax Python driver to a version that is capable of connecting to Astra databases created using DataStax Astra.

Prerequisites

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

Procedure

  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.

  2. 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
  3. In your existing DataStax Python driver code, modify the connection code to use the DataStax Astra API.

    In the cloud_config parameter, include the path to the secure connect bundle for your Astra 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('username', 'password')
      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='username', 'password')
      cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
      session = cluster.connect()
  4. Run your Python script to connect to your Astra database.
    python ./my_python_driver.py

    If successful, the code builds, compiles, and connects to your Cassandra database.