“Apache Cassandra supports triggers written in any JVM language. Triggers are stored outside the database, and the code is stored in the /lib/triggers directory within the Cassandra installation directory. Once Cassandra starts up, it loads the trigger and applies it to every node in the cluster.”
Cassandra Create Trigger Command
To create a trigger in Cassandra, we use the CREATE TRIGGER command in the CQL shell. The command syntax is as shown:
ON table_name
USING 'string'
You can use the IF NOT EXISTS command to suppress any errors if a trigger with a similar name exists on the database.
You can specify the trigger_name without any quotation marks. However, Cassandra will not preserve the casing if the trigger name contains them. Therefore, to keep the casing, ensure to enclose the trigger_name with quotation marks.
Using keywords specifies the class that implements the trigger. These classes are defined in the Apache Trigger repository as shown in the resource below:
https://github.com/apache/cassandra/tree/trunk/examples/triggers/src/org/apache/cassandra/triggers
Remember that a trigger defined on a table will execute before a requested DML statement.
NOTE: In the recent Cassandra version, triggers are located in the install_dir/conf/triggers directory. Place any triggers in the directory as .jar files.
Example
To illustrate how to create a trigger, consider the source shown below:
https://github.com/apache/cassandra/tree/trunk/examples/triggers
We can create a trigger as shown:
The statement should create an audit trigger, as shown in the Apache example.