Guest User

Untitled

a guest
Dec 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. """
  2. Import the cassandra libraries that will be needed to connect to the cassandra cluster.
  3.  
  4. Note: Currently the cassandra-driver is only available for python 2.7 on conda-forge
  5.  
  6. Example install of package
  7. conda install -c conda-forge cassandra-driver
  8. """
  9. from cassandra.auth import PlainTextAuthProvider
  10. from cassandra.cluster import Cluster
  11.  
  12.  
  13. import json
  14.  
  15.  
  16. # Get credentials from kubernetes. The credentials were setup as a dictionary
  17. credentials = None
  18. with open('/var/run/secrets/user_credentials/cassandra_credentials') as f:
  19. credentials = json.load(f)
  20.  
  21. # Check and make sure the credentials were pulled correctly
  22. if credentials:
  23. # Setup authentication mechanism
  24. auth_provider = PlainTextAuthProvider(
  25. username=credentials.get('username'),
  26. password=credentials.get('password')
  27. )
  28.  
  29. # Pass parameters to the cluster
  30. cluster = Cluster(
  31. auth_provider=auth_provider,
  32. contact_points=['support-cassandra.dev.anaconda.com']
  33. )
  34.  
  35. # COnnect to cluster and set the keyspace
  36. session = cluster.connect()
  37. session.set_keyspace('quote')
  38.  
  39. # Run query in keyspace and print out the results
  40. rows = session.execute('SELECT * FROM historical_prices')
  41. for row in rows:
  42. print(row)
  43.  
  44. # Disconnect from the cluster
  45. cluster.shutdown()
Add Comment
Please, Sign In to add comment