Using the DataStax Python driver

Use the DataStax Python driver to connect to your database created using the DataStax Constellation console.

The DataStax Python driver provides a framework you can customize to begin developing and testing your own applications. Use the DataStax Python driver to connect with an Apollo database that you specify.

Connecting to your database with the DataStax Python driver

Use the DataStax Python driver to connect to your cluster created using the DataStax Constellation console.

Use the DataStax Python driver to connect to your Apollo database and begin building your own application.
Tip: DataStax recommends using the DataStax Python driver for Apache Cassandra. You can also use the DataStax Enterprise (DSE) Python driver, which exposes the same API for connecting to Cassandra databases.

Prerequisites

  1. Install pyenv, Python, and virtualenv.
  2. Create a DataStax Apollo database.
  3. Download the secure connect bundle to obtain connection credentials for your database.

Procedure

  1. Create and activate your virtual environment.
    pyenv virtualenv 2.7.16 my_environment
    pyenv activate my_environment
  2. 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.

  3. 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
  4. Create a connect_database.py file in the main directory for your Python project.
    cd python_project
    touch connect_database.py
  5. Copy the following connection code into the connect_database.py file.
    Note: 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 Apollo database credentials (secure-connect-database_name.zip).
    • DataStax Python driver for Apache Cassandra

      from cassandra.cluster import Cluster
      from cassandra.auth import PlainTextAuthProvider
      
      cluster = Cluster(
          cloud={
              'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
          },
          auth_provider=PlainTextAuthProvider('username', 'password')
      )
      session = cluster.connect()
    • DSE Python driver

      from dse.cluster import Cluster
      from dse.auth import PlainTextAuthProvider
      
      cloud_config = {
          'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
      }
      cluster = Cluster(cloud=cloud_config, auth_provider=PlainTextAuthProvider('username', 'password'))
      session = cluster.connect()
  6. After the connection code, add the following code to connect-database.py. This code creates a Cluster instance to connect to your Apollo 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.")
  7. Save and close the connect_database.py file.
  8. Run connect_database.py.
    python ./connect_database.py

    The console output displays the release_version from the system.local table:

    4.0.0.670

What's next

Build your application. See the DataStax Python driver for Apache Cassandra and DSE Python driver documentation for more information about using the DataStax Python driver.

Installing pyenv, Python, and virtualenv

Install pyenv and virtualenv to manage Python without impacting your system installation.

Instead of installing Python directly on your machine, install pyenv to manage Python. This method ensures that your system installation of Python is unaffected by Python versions that the DataStax Python driver requires.

Install pyenv, and then install virtualenv. With that software installed, create a virtual environment and install Python in the virtual environment you created. You can install the DSE Python driver outside of virtualenv, but the following installation method is strongly recommended.

Note: This method of managing Python is optimal for Linux and macOS users.

Procedure

  1. Install pyenv from Git. The following command installs pyenv in the $HOME/.pyenv directory.
    git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  2. Linux: Install the following packages that pyenv requires. The libssl-dev package is required when installing Python.
    sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
    xz-utils tk-dev libffi-dev
  3. Install virtualenv from Git.
    git clone https://github.com/pyenv/pyenv-virtualenv.git \
      $HOME/.pyenv/plugins/pyenv-virtualenv
  4. Add the following lines to your profile for pyenv and virtualenv. The following command uses nano to modify the .bashrc file, but use your preferred editing tool.
    Note: If you did not install pyenv in $HOME/.pyenv, modify PYENV_ROOT to point to the pyenv installation directory.
    nano ~/.bashrc
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"

    The previous lines set your root directory for pyenv, add that directory to your system PATH, initialize pyenv, and start virtualenv.

  5. Source the changes to your profile. You must source your profile in each terminal session where you want to run pyenv and virtualenv.

    Typically, the .bashrc file is sourced when starting a new terminal session, but explicitly sourcing your profile ensures that the additions in 4 take effect.

    source ~/.bashrc
    Tip: If you use zsh, modify ~/.zshrc instead of ~/.bashrc.
  6. Use pyenv to install the latest Python 2.7.x version.
    sudo pyenv install 2.7.16
    Note: Python 2.7, 3.4, 3.5 and 3.6 are supported. CPython (the standard Python implementation) and PyPy are supported and tested.
  7. Create and activate your virtual environment.
    pyenv virtualenv 2.7.16 my_environment
    pyenv activate my_environment
  8. Optional: If pip is not already installed, then install pip:
    1. Securely download get-pip.py:
      curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    2. Install get-pip.py:
      sudo python get-pip.py

      See the pip installation instructions for more information.