[英]How to tell the difference between Insert and Update in Cosmos DB change feed
I did get the code sample for the Cosmos DB change feed from this resource我确实从该资源中获得了 Cosmos DB 更改提要的代码示例
I was able to successfully compile and run the code.我能够成功编译并运行代码。
Here is the code for the change feed call这是更改提要调用的代码
private static async Task<ChangeFeedProcessor> StartChangeFeedProcessorAsync(
CosmosClient cosmosClient,
IConfiguration configuration)
{
string databaseName = "changefeedsample";// configuration["SourceDatabaseName"];
string sourceContainerName = "source";// configuration["SourceContainerName"];
string leaseContainerName = "leases";// configuration["LeasesContainerName"];
Container leaseContainer = cosmosClient.GetContainer(databaseName, leaseContainerName);
ChangeFeedProcessor changeFeedProcessor = cosmosClient.GetContainer(databaseName, sourceContainerName)
.GetChangeFeedProcessorBuilder<ToDoItem>(processorName: "changeFeedSample", onChangesDelegate: HandleChangesAsync)
.WithInstanceName("consoleHost")
.WithLeaseContainer(leaseContainer)
.Build();
Console.WriteLine("Starting Change Feed Processor...");
await changeFeedProcessor.StartAsync();
Console.WriteLine("Change Feed Processor started.");
return changeFeedProcessor;
}
Here is the code for the action taken on a change feed这是对更改提要采取的操作的代码
static async Task HandleChangesAsync(IReadOnlyCollection<ToDoItem> changes, CancellationToken cancellationToken)
{
Console.WriteLine("Started handling changes...");
foreach (ToDoItem item in changes)
{
Console.WriteLine($"Detected operation for item with id {item.id}, created at {item.creationTime}.");
// Simulate some asynchronous operation
await Task.Delay(10);
}
Console.WriteLine("Finished handling changes.");
}
I do see how the action can be triggered on an insert;我确实看到了如何在插入时触发操作; however, is there a way to tell if there was an action taken on update.
但是,有没有办法判断是否对更新采取了行动。 Is there a way to tell which is which, to tell one from another.
有没有办法分辨哪个是哪个,从另一个中分辨出来。 Is there a way to get more details on an updated/added data
有没有办法获得有关更新/添加数据的更多详细信息
Thank you very much in advance非常感谢您提前
Is there a way to tell which is which
有没有办法分辨哪个是哪个
The CosmosDb change feed works by sending you the current value of the document. CosmosDb 更改提要通过向您发送文档的当前值来工作。 Because of this, you can't distinguish between inserts or edits.
因此,您无法区分插入或编辑。
If you only need to distinguish inserts and edits, then you can add a bool IsEdited
field to your object and set it to true when the document is edited.如果您只需要区分插入和编辑,那么您可以在您的对象中添加一个
bool IsEdited
字段,并在编辑文档时将其设置为 true。 This is sufficient for doing something like a summary/count table where you want to process inserts but ignore edits.这足以执行诸如要处理插入但忽略编辑的汇总/计数表之类的操作。
If you need to process edits also, then you'll need to detect multiple edits, not just the first one.如果您还需要处理编辑,则需要检测多个编辑,而不仅仅是第一个。 In that case, you would need a second container that has the document id and last modified time (
_ts
), and compare the incoming _ts
against the one you already saw (if any).在这种情况下,您将需要具有文档 ID 和上次修改时间 (
_ts
) 的第二个容器,并将传入的_ts
与您已经看到的(如果有)进行比较。 You'll probably need a JavaScript stored procedure to do that compare+update atomically.您可能需要一个JavaScript 存储过程来以原子方式进行比较+更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.