我遇到了非常奇怪的行为,并试图了解在哪些情况下可能会发生这种情况。在我的Python应用程序中,我通过驱动程序访问Cassandra数据库。
正如你在下面看到的,首先,我做了一个INSERT
操作,它在表中创建了一条记录。接下来,我执行一个SELECT
操作,该操作应返回之前创建的最后一条消息。有时选择操作会向我返回空值。我假设 Cassandra 有一个内部调度程序来执行 INSERT 任务。但是,当我尝试通过 SELECT 操作获取最后一条记录时,该记录尚未创建。这可能吗?
问题:
是否可以在成功创建记录的 INSERT 操作后从 Cassandra 获得回调?
片段:
import uuid
import sys
from cassandra import ConsistencyLevel
from cassandra.query import SimpleStatement, dict_factory
def send_message(chat_room_id, message_author_id, message_text):
message_id = uuid.uuid1()
first_query = """
insert into messages (
created_date_time,
chat_room_id,
message_id,
message_author_id,
message_text
) values (
toTimestamp(now()),
{0},
{1},
{2},
{3}
);
""".format(
chat_room_id,
message_id,
message_author_id,
message_text
)
first_statement = SimpleStatement(
first_query,
consistency_level=ConsistencyLevel.LOCAL_QUORUM
)
try:
db_connection.execute(first_statement)
except Exception as error:
logger.error(error)
sys.exit(1)
db_connection.row_factory = dict_factory
second_query = """
select
created_date_time,
chat_room_id,
message_id,
message_author_id,
message_text
from
messages
where
chat_room_id = {0}
and
message_id = {1}
limit 1;
""".format(
chat_room_id,
message_id
)
try:
message = db_connection.execute(second_query).one()
except Exception as error:
logger.error(error)
sys.exit(1)
print(message) # Sometimes when it's the first message in the chat room I see a "None" value.