Cassandra python 驱动程序:客户端请求超时

Mar*_*es 3 cassandra python-2.7 datastax

我设置了一个简单的脚本来将新记录插入到 Cassandra 数据库中。它在我的本地机器上运行良好,但是当我将数据库移动到远程机器时,我从客户端收到超时错误。如何正确设置此驱动程序的超时时间?我尝试了很多东西。我在我的 IDE 中破解了超时并让它在没有超时的情况下工作,所以我确信这只是一个超时问题。

我如何设置我的集群:

profile = ExecutionProfile(request_timeout=100000)
self.cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], auth_provider=auth_provider,
                       execution_profiles={EXEC_PROFILE_DEFAULT: profile})
connection.setup(hosts=[os.getenv('CASSANDRA_SEED', None)],
                 default_keyspace=os.getenv('KEYSPACE', None),
                 consistency=int(os.getenv('CASSANDRA_SESSION_CONSISTENCY', 1)), auth_provider=auth_provider,
                 connect_timeout=200)

session = self.cluster.connect()
Run Code Online (Sandbox Code Playgroud)

我正在尝试执行的查询:

    model = Model.create(buffer=_buffer, lock=False, version=self.version)
Run Code Online (Sandbox Code Playgroud)

13. . ': '客户端请求超时。参见 Session.execute_async'}, last_host=54.213。.

我插入的记录是 11mb,所以我可以理解有延迟,只是增加超时应该可以做到,但我似乎无法弄清楚。

Ian*_*dby 6

默认请求超时是 Session 对象的一个​​属性(驱动程序的 2.0.0 版及更高版本)。

session = cluster.connect(keyspace)
session.default_timeout = 60
Run Code Online (Sandbox Code Playgroud)

这是最简单的答案(无需弄乱执行配置文件),我已经确认它有效。

https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Session


小智 5

根据驱动程序 3.x 版本的文档, 是classrequest_timeout的一个属性,您可以为集群构造函数提供执行配置文件(这里是一个示例)。ExecutionProfile

所以,你可以这样做:

from cassandra.cluster import Cluster
from cassandra.cluster import ExecutionProfile

execution_profil = ExecutionProfile(request_timeout=600)
profiles = {'node1': execution_profil}
cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], execution_profiles=profiles)
session = cluster.connect()

session.execute('SELECT * FROM test', execution_profile='node1')
Run Code Online (Sandbox Code Playgroud)

重要提示:
当您使用execute或时execute_async,您必须指定execution_profile名称。


Sim*_*son 4

您可以在 Cluster 构造函数中设置 request_timeout :

self.cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], 
                       auth_provider=auth_provider,
                       execution_profiles={EXEC_PROFILE_DEFAULT: profile},
                       request_timeout=10)
Run Code Online (Sandbox Code Playgroud)

参考: https: //datastax.github.io/python-driver/api/cassandra/cluster.html

  • 你不能,这是一个意想不到的参数,并且会出错。 (2认同)
  • 从 cassandra-driver 3.27 开始,它已移至“ExecutionProfile”:https://docs.datastax.com/en/developer/python-driver/3.28/api/cassandra/cluster/#cassandra.cluster.ExecutionProfile (2认同)