简体   繁体   English

将表 Mutation 插入到不同的 Cassandra 表

[英]Insert table Mutation to different Cassandra table

I have requirement to keep the old values of a row in a history table for auditing whenever we do row update.每当我们进行行更新时,我都需要将行的旧值保留在历史表中以供审核。 Is there any solution available in Apache Cassandra to achieve this? Apache Cassandra 中是否有可用的解决方案来实现这一目标? I looked at the Trigger and not much mentioned in the docs.我查看了触发器,在文档中没有太多提及。 Not sure of performance issues if we use the triggers.如果我们使用触发器,不确定性能问题。 Also if we use trigger, will it give the old value for a column when we do update?此外,如果我们使用触发器,它会在我们更新时为列提供旧值吗?

Cassandra is best tool to keep the row history. Cassandra 是保留行历史记录的最佳工具。 I will try to explain it with an example.我将尝试用一个例子来解释它。 Consider the below table design -考虑下表设计 -

CREATE TABLE user_by_id (
userId text,
timestamp timestamp,
name text,
fullname text,
email text,
PRIMARY KEY (userId,timestamp)
) WITH CLUSTERING ORDER BY (timestamp DESC);

With this kind of table design you can keep the history of the record.通过这种表格设计,您可以保留历史记录。 Here, userid is row partition key and timestamp as clustering key.这里,userid 是行分区键,时间戳是集群键。 Every insert for same user will be recorded as different row.同一用户的每次插入都将记录为不同的行。 for example -例如 -

insert into user_by_id (userId,timestamp ,name, fullname, email ) values ('1',<newTimeStamp>,'x',xyz,'x@xyz.com');
insert into user_by_id (userId,timestamp ,name, fullname, email ) values ('1',<newTimeStamp>,'y',xyz,'y@xyz.com');
insert into user_by_id (userId,timestamp ,name, fullname, email ) values ('1',<newTimeStamp>,'z',xyz,'z@xyz.com');

Above insert statements are actually updating values of the name and email column.上面的插入语句实际上是更新名称和 email 列的值。 But, this will be saved in three different rows because of timestamp as a clustering key, timestamp will be different for each row.但是,由于时间戳作为聚类键,这将保存在三个不同的行中,每行的时间戳都是不同的。 If you want to get the latest value, just use LIMIT in your select query.如果要获取最新值,只需在 select 查询中使用 LIMIT。

This design keeps the history of the row which can be used foe audit purpose.这种设计保留了可用于审计目的的行的历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM