【问题标题】:Unable to connect cassandra with python无法将 cassandra 与 python 连接
【发布时间】:2014-09-23 06:26:18
【问题描述】:

我正在尝试从 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。 有什么问题,不胜感激。

【问题讨论】:

  • 您的 cassandra.yaml 中的 listen_address 设置为什么?
  • @BryceAtNetwork23,监听地址默认为localhost:9160
  • 您不应将端口指定为listen_address 的一部分。
  • 对于仍在寻找示例代码的任何人,我在这里发布了一个:github.com/hardikvasa/database-journal

标签: python cassandra pycassa


【解决方案1】:

也许可以尝试指定主机:

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

【讨论】:

  • 还有...您不使用 datastax 驱动程序的原因是什么? [我知道这没有用,但由于官方数据税已经发布,如果我刚开始,我会使用它们。]
  • 我也从这个 url pycassa.github.io/pycassa/tutorial.html#connecting-to-cassandra 尝试过,但没有解决
  • 你能看到 cassandra 是否通过 nodetool 运行吗?网络端口是否打开?您能否也提供您的 cassandra.yaml 文件?
  • 那个端口没有运行
  • 您需要将连接池指向至少一个正在运行的、可访问的 cassandra 节点才能使其工作。
【解决方案2】:

将 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2018-12-16
    • 2018-09-22
    • 2016-04-03
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多