Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Cassandra example connection
Import the cassandra libraries that will be needed to connect to the cassandra cluster.
Note: Currently the cassandra-driver is only available for python 2.7 on conda-forge
Example install of package
conda install -c conda-forge cassandra-driver
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
import json
# Get credentials from kubernetes. The credentials were setup as a dictionary
credentials = None
with open('/var/run/secrets/user_credentials/cassandra_credentials') as f:
credentials = json.load(f)
# Check and make sure the credentials were pulled correctly
if credentials:
# Setup authentication mechanism
auth_provider = PlainTextAuthProvider(
# Pass parameters to the cluster
cluster = Cluster(
# COnnect to cluster and set the keyspace
session = cluster.connect()
# Run query in keyspace and print out the results
rows = session.execute('SELECT * FROM historical_prices')
for row in rows:
# Disconnect from the cluster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment