我正在尝试从python连接到cassandra,我已将cassandra
安装为pip install pycassa
。当我尝试连接到cassandra
时,我收到以下异常
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
我正在使用python 2.7。 有什么问题,我们将不胜感激。
答案 0 :(得分:0)
也许尝试指定主机:
pool = ConnectionPool('Keyspace1',['server_node_here:9160'])
答案 1 :(得分:0)
将Cassandra与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')
您还可以使用带有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
有关模型类实现的详细信息,请看:https://github.com/vishal-kr-yadav/NoSQL_Databases