Unable to connect cassandra with python

2.3k Views Asked by At

I am trying to connect to cassandra from python , I have installed cassandra as pip install pycassa.When i am trying to connect to the cassandra i am getting the following exception

from pycassa.pool import ConnectionPool
pool = ConnectionPool('Keyspace1')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/pycassa/pool.py", line 382, in __init__
self.fill()
File "/usr/lib/python2.7/site-packages/pycassa/pool.py", line 442, in fill
conn = self._create_connection()
File "/usr/lib/python2.7/site-packages/pycassa/pool.py", line 431, in _create_connection
(exc.__class__.__name__, exc))
pycassa.pool.AllServersUnavailable: An attempt was made to connect to each of the servers twice, but none of the attempts succeeded. The last failure was TTransportException: Could not connect to localhost:9160

I am using python 2.7. What is the problem, Any help would be appreciated.

2

There are 2 best solutions below

6
ashic On

Perhaps try specifying the host:

pool = ConnectionPool('Keyspace1', ['server_node_here:9160'])

0
vishal yadav On

General way to connect Cassandra with python.

from cassandra.cluster import Cluster  
cluster = Cluster()    #for connecting on localhost  
cluster = Cluster(['192.168.0.1', '192.168.0.2']) #*for connecting on clusters (comment this line, if you are connecting with localhost)*  
session = cluster.connect('testing')

You can also connect using model class with python

from cassandra.cqlengine import columns  
from cassandra.cqlengine.models import  Model  
from cassandra.cqlengine.management import sync_table  
from cassandra.cqlengine import connection  
import uuid  
from datetime import datetime  

connection.setup(['127.0.0.1'], "testing")  #testing is the keyspace

For detail information for model class implementation take a look: https://github.com/vishal-kr-yadav/NoSQL_Databases