如何通过 ChangeFeed 获得有关 Cosmos DB 更改的通知?

How to get notified about change in Cosmos DB through ChangeFeed?

当我浏览 MSDN documentation 时,我只能看到 ChangeFeed 上的 "observing" 变化。即使是那里的第一个图表也只显示方向 来自 外部服务(Storm、Azure Functions 等)towards ChangeFeed。

是否有任何模式(对我来说不是很明显)我们可以用来通过 ChangeFeed 获得有关 Cosmos DB 更改的通知? 或者确实需要内置支持,例如Azure Functions 让这个 "PUSH" 场景正常工作?

非常感谢

最近发布了 Azure Cosmos DB Change Feed processor library,以便更轻松地收听 Azure Cosmos DB 更改源。它处理一组更改提要侦听 "workers" 的自动租赁管理,以跨 Cosmos DB 分区读取;使您能够轻松扩展更改提要侦听工作人员的集合。使用它,您可以在库的使用者之上构建一个 pub/sub 模型。

话虽如此,Azure Cosmos DB 的变更源与 Azure Functions 之间的集成已在 Azure Cosmos DB 路线图上(截至当前日期 - 2017 年 8 月 5 日),以帮助更轻松地完成推送变更。目前还没有公布具体日期;但请继续关注。

想对这个问题提供一些更新,因为现在已经添加了 Azure Functions 支持,并且 Change Feed Processor 库已经完全集成到 Java/ 的 Azure SQL SDK 中。净

在不使用 Azure Functions 的情况下,通过使用 ChangeFeedProcessor API 从更改源获取通知真的很容易 - 但请记住,它并不是真正的“PUSH " 场景,因为客户端以轮询方式查询更改提要!

代码如下所示:

public static void startChangeFeedProcessor(String hostName, CosmosContainer feedContainer, CosmosContainer leaseContainer) {
  ChangeFeedProcessor.Builder()
    .hostName(hostName)
    .feedContainer(feedContainer)
    .leaseContainer(leaseContainer)
    .options(new ChangeFeedProcessorOptions().leasePrefix("my-lease-prefix")) //not required
    .handleChanges(docs -> {
        for (CosmosItemProperties document : docs) {
            System.out.println("Read document from Change Feed: " + document.toJson(SerializationFormattingPolicy.INDENTED));
        }
    })
    .build()
    .start()
    .subscribe();
}

您可以查看more in-depth details about the Azure CosmosDB Change Feed here