Cassandra 3.11 triggers How get old value of updated field?

时间:2018-09-18 20:14:16

标签: triggers cassandra-3.0

i started experimenting with Cassandra triggers and i am trying to populate a table auditTable when a table test is being updated:

i want the auditTable to show the updated column's name, the old value , the new value

how can i get the old value for the updated field? thanks

public Collection<Mutation> augment(Partition update)
 {
   String auditKeyspace = properties.getProperty("keyspace");
   String auditTable = properties.getProperty("table");

CFMetaData metadata = Schema.instance.getCFMetaData(auditKeyspace, auditTable);

PartitionUpdate.SimpleBuilder audit = PartitionUpdate.simpleBuilder(metadata, UUIDGen.getTimeUUID());


try {
    UnfilteredRowIterator it = update.unfilteredIterator();
    while (it.hasNext()) {
      Unfiltered unfiltered = it.next();
      Clustering clt = (Clustering) unfiltered.clustering();
      Iterator<Cell> cells = update.getRow(clt).cells().iterator();
      Iterator<ColumnDefinition> columns = update.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

            if (cell.isTombstone()) {
              // Cell deletion
              audit.row().add("operation", "CELL DELETION");
            }
            else {
              // Insert or Update
              audit.row().add("operation", "CELL UPDATE");
              audit.row().add("newvalue", cell.value());
              audit.row().add("oldvalue", "****WHAT SHOULD COME HERE???****");
              audit.row().add("updatedfield", columnDef.name.toString());
            }
            if (columnDef.name.toString().equalsIgnoreCase("updatedby"))
              audit.row().add(columnDef.name.toString(), data);
          }

} catch (Exception e) {

}

audit.row()
    .add("keyspace_name", update.metadata().ksName)
    .add("table_name", update.metadata().cfName)
    .add("primary_key", update.metadata().getKeyValidator().getString(update.partitionKey().getKey()));


return Collections.singletonList(audit.buildAsMutation());

}

0 个答案:

没有答案