问题
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?
回答1:
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();
}
}
来源:https://stackoverflow.com/questions/35161747/example-of-creating-triggers-in-cassandra-and-does-this-support-only-for-java