使用 python 初始化 cassandra 集群

问题描述 投票:0回答:1

我正在使用此 python 代码块初始化 cassandra 集群:

cluster = Cluster(['172.31.29.49', '172.31.47.96'])
session = cluster.connect()
create_cassandra_table(session)

首先,我输入了这个地址 127.0.0.1,然后我收到了这个错误:

    File "/home/ubuntu/snapshot.py", line 125, in <module>
    store_docker_checkpoints()
    File "/home/ubuntu/snapshot.py", line 94, in store_docker_checkpoints
    session = cluster.connect()
    File "cassandra/cluster.py", line 1717, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 1753, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 1740, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 3543, in cassandra.cluster.ControlConnection.connect
    File "cassandra/cluster.py", line 3588, in cassandra.cluster.ControlConnection._reconnect_internal
    cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1:9042':    ConnectionRefusedError(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})

然后我用两个节点的 IPv4 私有地址更改了集群。我使用的是两节点集群。每个节点都是一个 EC2 实例。 当我运行

nodetool status
时一切正常,但当我运行脚本时出现此错误。

python cassandra
1个回答
0
投票

当我运行你的代码时(知道IP是私有的,我将无法看到它们):

cluster = Cluster(['172.31.29.49', '172.31.47.96'])
session = cluster.connect()

我收到这条消息:

  File "/Users/aaron.ploetz/Documents/workspace/DS_Python_stuff/testCassandra.py", line 27, in <module>
    session = cluster.connect()
  File "cassandra/cluster.py", line 1677, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1713, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1700, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 3507, in cassandra.cluster.ControlConnection.connect
  File "cassandra/cluster.py", line 3552, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'172.31.47.96:9042': OSError(None, "Tried connecting to [('172.31.47.96', 9042)]. Last error: timed out"), '172.31.29.49:9042': OSError(None, "Tried connecting to [('172.31.29.49', 9042)]. Last error: timed out")})

因此它清楚地知道它尝试连接的地址,而不是默认为 127.0.0.1。

我猜测您对

snapshot.py
的更改没有被保存,或者您尝试运行的脚本没有您的更改。

此外,在命令行上将 IP(和其他运行时参数)设置为环境变量或参数是一个更好的主意。这样您就不必对 IP 地址进行硬编码。

© www.soinside.com 2019 - 2024. All rights reserved.