Search code examples
cassandracqlcassandra-3.0

How to implement trigger in Cassandra?


Is there any good example or guide on how to implement a Cassandra trigger step-by-step?

I'm looking for something advanced, like manipulating other rows in same partition after an INSERT or UPDATE is performed.

Unfortunately the documentation is very limited, I only found these links so far:

Example

Let's say you want to put logs into a table and aggregate them whenever you reach a certain number of log entries.

CREATE TABLE simple_log (
    id int,
    event_id timeuuid,
    message text,
    PRIMARY KEY(id)
);

INSERT INTO simple_log (id, event_id, message) VALUES (1, now(), 'first');
INSERT INTO simple_log (id, event_id, message)  VALUES (1, now(), 'second');
INSERT INTO simple_log (id, event_id, message)  VALUES (1, now(), 'third');

Whenever you select top the logs by ID, you should get back an aggregated result:

SELECT * FROM simple_log WHERE id = 1;

 id | event_id                             | message
----+--------------------------------------+---------
  1 | d97f92d1-b5a4-11e7-825c-495e6ea8608a |   first-second-third

Solution

  • You can add mutations to manipulate other rows into the collection of mutations like in the example.

    Both it and CDC do not have a whole lot of documentation around it because its generally recommended not to use them unless really needed. Theres a lot of complexity in distributed systems with things like this and it can usually be reasoned better in your application layer.

    Why not just include the other mutations in an batch? especially in same partition it will likely work far better and require less effort in things like keeping trigger deployed on all instances in sync and updated etc.