想要查看Cassandra中的触发器功能。有人可以提供一个创建触发器的示例。
从这篇博客来看, http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-0-prototype-triggers-support
要创建触发器,必须首先使用实现ITrigger
接口的类构建jar并将其放入每个节点的triggers目录中,然后执行CQL3 CREATE TRIGGER
请求以绑定触发器到Cassandra表(或几个表)。
根据此信息,Cassandra中的触发器仅适用于基于Java的应用程序?
答案 0 :(得分:4)
Cassandra 3.0
你可以使用它,它会把插件中的所有东西都当作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();
}
}