virtualenv -p python3 venv
Connect to Cassandra with Python 3.x and get Pandas Dataframe
Upasana | February 13, 2020 | 2 min read | 523 views
We will be learning
-
How to connect to Cassandra in Python
-
How to get output of query as Pandas Dataframe
Connect to Cassandra in Python
We will be using cassandra
Library to make connection.
Install library to connect to Cassandra
Create virtual environment in project directory
Activate virtual environment
source venv/bin/activate
To know more about virtual environment, click here
Install package
pip install cassandra-driver
Define a class for connecting to Cassandra
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import pandas as pd
class CassDao(object):
def __init__(self):
self.cluster = None
self.username = cassandra_config['username'] //(1)
self.password = cassandra_config['password'] //(2)
self.host = cassandra_config['host'] //(3)
self.keyspace = cassandra_config['keyspace'] //(4)
self.session = self.createSession()
def __del__(self):
self.cluster.shutdown()
def createSession(self):
self.cluster = Cluster([self.host],
auth_provider=PlainTextAuthProvider(username=self.username, password=self.password))
self.session = self.cluster.connect(self.keyspace)
return self.session
def getSession(self):
return self.session
def get_data(self):
query = """SELECT id from '{}'.table """.format(self.keyspace)
data = pd.DataFrame(self.getSession().execute(query, timeout=None))
return data
-
Database Username
-
Password of DB
-
Host of database
-
keyspace
Note : For getting data, define the method in class itself.
main.py
dataclass = CassDao()
data = dataclass.get_data()
Output of data will be a pandas dataframe. Happy working with Cassandra now!
In case, you are looking to connect to
Top articles in this category:
- Connect to MySQL with Python 3.x and get Pandas Dataframe
- Connect to Postgresql with Python 3.x and get Pandas Dataframe
- Google Colab: import data from google drive as pandas dataframe
- Top 100 interview questions on Data Science & Machine Learning
- Google Data Scientist interview questions with answers
- Python - Get Google Analytics Data
- Python send GMAIL with attachment
Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..