Arun
Arun

Reputation: 1752

Example of creating triggers in Cassandra and does this support only for Java?

Wanted to check about Triggers feature in Cassandra. Can someone please provide an example for creating Trigger.

From this blog, http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-0-prototype-triggers-support

To create a trigger, you must first build a jar with a class implementing the ITrigger interface and put it into the triggers directory on every node, then perform a CQL3 CREATE TRIGGER request to tie your trigger to a Cassandra table (or several tables).

As per this info, Triggers in Cassandra are only applicable for Java based applications?

Upvotes: 2

Views: 7343

Answers (1)

Amrit Bains
Amrit Bains

Reputation: 111

Cassandra 3.0

You can use this and it will get you everything in the insert as a json

public class HelloWorld implements ITrigger
{
    private static final Logger logger = LoggerFactory.getLogger(HelloWorld.class);

    public Collection<Mutation> augment(Partition partition)
    {
        String tableName = partition.metadata().cfName;
        logger.info("Table: " + tableName);

        JSONObject obj = new JSONObject();
        obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));

        try {
            UnfilteredRowIterator it = partition.unfilteredIterator();
            while (it.hasNext()) {
                Unfiltered un = it.next();
                Clustering clt = (Clustering) un.clustering();  
                Iterator<Cell> cells = partition.getRow(clt).cells().iterator();
                Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();

                while(columns.hasNext()){
                    ColumnDefinition columnDef = columns.next();
                    Cell cell = cells.next();
                    String data = new String(cell.value().array()); // If cell type is text
                    obj.put(columnDef.toString(), data);
                }
            }
        } catch (Exception e) {

        }
        logger.debug(obj.toString());

        return Collections.emptyList();
    }
}

Upvotes: 5

Related Questions