我设置了一个简单的脚本来将新记录插入到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()
我正在尝试执行的查询:
model = Model.create(buffer=_buffer, lock=False, version=self.version)
13 ..':'客户请求超时。请参阅Session.execute_async'},last_host = 54.213 ..
我插入的记录是11mb,所以我可以理解有一个延迟,只是增加超时应该这样做,但我似乎无法搞清楚。
您可以在Cluster构造函数中设置request_timeout:
self.cluster = Cluster([os.getenv('CASSANDRA_NODES', None)],
auth_provider=auth_provider,
execution_profiles={EXEC_PROFILE_DEFAULT: profile},
request_timeout=10)
参考:https://datastax.github.io/python-driver/api/cassandra/cluster.html
根据文档,request_timeout
是ExecutionProfile
class的一个属性,您可以为集群构造函数(this is an example)提供执行配置文件。
所以,你可以这样做:
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')
重要提示:当您使用execute
或èxecute_async
时,您必须指定execution_profile
名称。
默认请求超时是Session对象的属性(驱动程序的版本2.0.0及更高版本)。
session = cluster.connect(keyspace)
session.default_timeout = 60
这是最简单的答案(不需要弄乱执行配置文件),我已经确认它有效。
https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Session