2
votes

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
1
I don't think there is a way. Cassandra doesn't have the rich option set that more mature database systems do. Also, consider the distributed nature of Cassandra. Where would the trigger run? What would the trigger be permitted to do?Marc Bernier
@MarcBernier - Why do they include it in the documentation then? I'm not sure if there's a plan to introduce richer trigger functionality, but in some cases when you want to extend database it can become useful.Oresztesz

1 Answers

2
votes

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.