我正在尝试实现一个 Cassandra 触发器,这样当 keyspace1.tableA 上有更新或删除时,触发器将向 keyspace1.tableB 添加一行。
tableB 中的列名与 tableA 中的列名完全不同。
我正在使用 Cassandra 2.1,无法迁移到更新的版本。查看https://github.com/apache/cassandra/blob/cassandra-2.1/examples/triggers/src/org/apache/cassandra/triggers/InvertedIndex.java上的 InvertedIndex 触发器示例,我可以看到添加突变:
从 InvertedIndex 示例:
for (Cell cell : update)
{
// Skip the row marker and other empty values, since they lead to an empty key.
if (cell.value().remaining() > 0)
{
Mutation mutation = new Mutation(properties.getProperty("keyspace"), cell.value());
mutation.add(properties.getProperty("columnfamily"), cell.name(), key, System.currentTimeMillis());
mutations.add(mutation);
}
}
挑战在于,在此示例中,传递给 mutation.add 的单元名称是 cell.name() ,它是一个现有对象,我们可以使用该函数获取其名称。
现在,我只是想存储对 tableA 进行更改的时间,所以 tableB 有两列:
- 更改时间timeuuid
- 操作文本
我需要添加一个突变,它将在 tableB 中添加一行,并执行更改时间和操作。如何在 Cassandra 2.1.12 中添加这样的行突变?
我已经尝试过了,但在触发器中出现空指针异常:
...
String keycol = "changetime";
ByteBuffer uuidKey = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
ColumnIdentifier ci = new ColumnIdentifier(keycol, false);
CellName cn = CellNames.simpleSparse(ci);
mutation = new Mutation(keyspace, uuidKey);
mutation.add(tableName,cn, uuidKey, System.currentTimeMillis());
...
任何帮助将不胜感激 - 我不了解 Cassandra 的内部结构,所以没有多少细节是太多的信息。