Skip to content

Instantly share code, notes, and snippets.

Embed
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(
username=credentials.get('username'),
password=credentials.get('password')
)
# Pass parameters to the cluster
cluster = Cluster(
auth_provider=auth_provider,
contact_points=['support-cassandra.dev.anaconda.com']
)
# COnnect to cluster and set the keyspace
session = cluster.connect()
session.set_keyspace('quote')
# Run query in keyspace and print out the results
rows = session.execute('SELECT * FROM historical_prices')
for row in rows:
print(row)
# Disconnect from the cluster
cluster.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment