Cassandra 设置一致性级别为quorum时抛出错误('必需参数不是整数')
这是我表的定义。
CREATE TABLE chat_data.twitch_chat_by_broadcaster_and_timestamp (
broadcaster_id int,
year_month int,
timestamp bigint,
message_id uuid,
message text,
PRIMARY KEY ((broadcaster_id, year_month), timestamp, message_id)
) WITH CLUSTERING ORDER BY (timestamp ASC, message_id ASC)
这是我用来插入数据的代码。
import logging
import auth.secrets as secrets
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from cassandra.policies import DCAwareRoundRobinPolicy, TokenAwarePolicy
from cassandra.query import BatchStatement, BatchType, tuple_factory
from datetime_helpers import get_month, get_next_month
class DatabaseConnection:
def __init__(self, keyspace):
auth_provider = PlainTextAuthProvider(
secrets.get_astra_client_id(), secrets.get_astra_secret()
)
load_balancing_policy = TokenAwarePolicy(DCAwareRoundRobinPolicy())
cluster = Cluster(
cloud=secrets.get_astra_cloud_config(),
auth_provider=auth_provider,
load_balancing_policy=load_balancing_policy,
)
self.session = cluster.connect(keyspace)
def insert_chats(self, messages):
logging.info(f"Inserting {len(messages)} message")
batch = BatchStatement(
consistency_level="QUORUM", batch_type=BatchType.UNLOGGED
)
statement = self.session.prepare(
"""
INSERT INTO twitch_chat_by_broadcaster_and_timestamp (broadcaster_id, year_month, timestamp, message_id, message)
VALUES (?, ?, ?, ?, ?)
"""
)
for m in messages:
broadcaster_id, timestamp, message_id, message = m
month = get_month(timestamp)
batch.add(
statement, (broadcaster_id, month, timestamp, message_id, message)
)
try:
self.session.execute(batch)
logging.info("Messages inserted successfully")
return True
except Exception as e:
logging.error(f"Exception: {e}")
return False
execute
报错了
Exception: ('Unable to complete the operation against any hosts', {<Host: <astra datastax machine #1>: error('required argument is not an integer'), <Host: <astra datastax machine #2>: error('required argument is not an integer'), <Host: <astra datastax machine #3>>: error('required argument is not an integer')}
我打印了在 batch.add
语句中传入的类型,并确认它们是正确的。最后,我通过从 BatchStatement 中移除 consistency_level="QUORUM"
,让数据库接受了我的插入。现在它变成了 batch = BatchStatement(batch_type=BatchType.UNLOGGED)
有没有人知道为什么我在尝试设置一致性级别为 quorum 时才会出现这个异常?另外,这个异常背后发生了什么?
1 个回答
1
你把一致性级别设置成了一个字符串值,而不是使用枚举 ConsistencyLevel.QUORUM
。这个枚举其实是一个整数值(你可以在这个链接找到更多信息:https://github.com/datastax/python-driver/blob/master/cassandra/__init__.py#L29)
batch = BatchStatement(
consistency_level=ConsistencyLevel.QUORUM, batch_type=BatchType.UNLOGGED
)