Apache Cassandra

Cassandra Create Trigger

“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:

CREATE TRIGGER IF NOT EXISTS trigger_name
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:

CREATE TRIGGER test1 ON test.test USING 'org.apache.cassandra.triggers.AuditTrigger';

The statement should create an audit trigger, as shown in the Apache example.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list