在我的Python ( 3.8 ) 应用程序中,我通过DataStax Python Driver 3.24向Cassandra数据库发出请求。
根据官方文档,我尝试通过BatchStatement使用单个查询执行几个 CQL 操作。不幸的是,我的代码导致以下内容出错:
"errorMessage": "retry_policy should implement cassandra.policies.RetryPolicy"
"errorType": "ValueError"
从我的代码中可以看出,我在reply_policy
里面设置了属性的值BatchStatement
。无论如何,我的代码会引发您在上面看到的错误。财产内部必须有什么样的价值reply_policy
?当前冲突的原因是什么?
代码片段:
from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
from cassandra.auth import PlainTextAuthProvider
from cassandra.policies import DCAwareRoundRobinPolicy
from cassandra import ConsistencyLevel
from cassandra.query import dict_factory
from cassandra.query import BatchStatement, SimpleStatement
from cassandra.policies import RetryPolicy
auth_provider = PlainTextAuthProvider(username=db_username, password=db_password)
default_profile = ExecutionProfile(
load_balancing_policy=DCAwareRoundRobinPolicy(local_dc=db_local_dc),
consistency_level=ConsistencyLevel.LOCAL_QUORUM,
request_timeout=60,
row_factory=dict_factory
)
cluster = Cluster(
db_host,
auth_provider=auth_provider,
port=db_port,
protocol_version=4,
connect_timeout=60,
idle_heartbeat_interval=0,
execution_profiles={EXEC_PROFILE_DEFAULT: default_profile}
)
session = cluster.connect()
name_1, name_2, name_3 = "Bob", "Jack", "Alex"
age_1, age_2, age_3 = 25, 30, 18
cql_statement = "INSERT INTO users (name, age) VALUES (%s, %s)"
batch = BatchStatement(retry_policy=RetryPolicy)
batch.add(SimpleStatement(cql_statement, (name_1, age_1)))
batch.add(SimpleStatement(cql_statement, (name_2, age_2)))
batch.add(SimpleStatement(cql_statement, (name_3, age_3)))
session.execute(batch)