0

在我的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)
4

1 回答 1

0

好吧,我终于找到了错误。

retry_policyBatchStatement. 然后我的错误是我将 CQL 参数放在SimpleStatement.

这是工作示例代码片段:

...
batch = BatchStatement(batch_type=BatchType.UNLOGGED)
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)

编辑:

结果,我BatchStatement在这篇文章底部留下评论后放弃了。我求你注意他们!CQL 批次与 RBDMS 批次不同。CQL 批处理不是一种优化,而是用于实现跨多个表的非规范化记录的原子更新。

于 2020-12-13T11:15:38.987 回答