python - 使用 Jupyter IPython 和 Cassandra 驱动程序

标签 python cassandra jupyter-notebook

我正在尝试将 ipython 笔记本与 cassandra python 驱动程序一起使用。使用命令行 ipython 完全没问题;我能够建立连接。但是,当我使用具有相同代码的 Jupyter IPython 笔记本时,遇到连接错误。

from cassandra.cluster import Cluster
cluster = Cluster( contact_points=['xxx.xxx.xxx.xxx', 'xxx.xxx.xxx.xxx'] )
session = cluster.connect()

我可以通过命令行使用 ipython 和 python 运行上述 3 行。在 jupyter ipython 笔记本中执行代码时出现错误:

WARNING:cassandra.cluster:Failed to create connection pool for new host 10.0.0.7

...

error: [Errno None] Tried connecting to [('10.0.0.7', 9042)]. Last error: None

WARNING:cassandra.pool:Error attempting to reconnect to 10.0.0.7, scheduling retry in 2.0 seconds: [Errno None] Tried connecting to [('10.0.0.7', 9042)]. Last error: None

(我正在使用 cassandra python 驱动程序 pip install cassandra-driver)

可能是 IP 地址或路由问题吗? “新主机”提到的错误消息指向本地 IP 地址,而不是我用来连接的 IP 地址。如果是这样的话,我想知道为什么 ipython 命令行与笔记本会产生不同的结果,所以它与笔记本处理连接的方式有关。有谁知道为什么会出现这种情况以及我如何修复或解决此连接错误?

最佳答案

连接问题与与内部 IP 地址通信的节点有关。遇到这个post这有助于澄清问题。

"When connecting to the cluster from external client using the the nodes' external IP addresses, these internal IPs gets returned as hosts, which makes the connection pool produce warnings since it can't connect to them."

仍然不确定为什么命令行和笔记本环境之间的行为不同,但我按照建议使用 WhiteListRoundRobinPolicy 解决了连接问题(显式指定集群中的公共(public) IP 地址)

from cassandra.cluster import Cluster
from cassandra.policies import WhiteListRoundRobinPolicy

lbp = WhiteListRoundRobinPolicy(['54.209.226.178', '52.7.220.112'])
cluster = Cluster( contact_points=['54.209.226.178', '52.7.220.112'], load_balancing_policy=lbp )
session = cluster.connect()

更多关于WhiteListRoundRobinPolicy

关于python - 使用 Jupyter IPython 和 Cassandra 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237523/

相关文章:

python - 使用 Excel 工作表创建字典以替换值

python - 更改 QTableView 中的默认行大小

java - 在 cassandra 中使用计数器来处理多个 java 实例

java - Cassandra : Getting read count for Index table in cassandra?

ssh - 为运行 Jupyter Notebook 的远程 docker 容器创建 ssh 隧道

widget - 如何在 iPython 3 中左对齐小部件标签?

python - 创建一个用于在 Python3 中运行二进制程序的最小沙箱

python - 使用子列表中可用的文本信息从 super 列表中提取元素

cassandra - 复制因子 3 但所有权总数仅为 100% 而不是 300%

python - 启动 ipython-notebook 时出现 "ERROR:tornado.application:Uncaught exception GET"