Created
December 10, 2018 19:06
-
-
Save oldarmyc/e706710593f5f28c10a6a065b05ca737 to your computer and use it in GitHub Desktop.
Cassandra example connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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