Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 10, 2018 19:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oldarmyc/e706710593f5f28c10a6a065b05ca737 to your computer and use it in GitHub Desktop.
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