我有一个无序事件列表,我的任务是为它们存储第一次和最后一次出现的时间。
我在Cassandra中有以下列族:
CREATE TABLE events (
event_name TEXT,
first_occurrence BIGINT,
last_occurrence BIGINT,
PRIMARY KEY (event_name)
);
如果我有一个名为“some_event”的事件,发生次数为123456,那么我想做的事情在MySQL术语中看起来像这样:
INSERT INTO events (event_name, first_occurence, last_occurence)
VALUES ('some_event', 123456, 123456)
ON DUPLICATE KEY UPDATE
first_occurrence = LEAST(first_occurrence, 12345),
last_occurrence = GREATEST(last_occurrence, 123456)
我将使用Cassandra中的轻量事务来实现这一目标,像这样:
INSERT INTO events(event_name, first_occurrence, last_occurrence) VALUES ('some_event', 12345, 12345) IF NOT EXISTS;
UPDATE events SET first_occurrence = 123456 WHERE event_name='some_event' IF first_occurrence > 123456;
UPDATE events SET last_occurrence = 123456 WHERE event_name='some_event' IF last_occurrence < 123456;
事实证明,CQL3在轻量级事务的IF子句中不允许使用<和>运算符。
那么我的问题是,如何进行这样的条件更新?