简体   繁体   中英

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

When I'm browsing through MSDN documentation all I can see is "observing" changes on ChangeFeed. Even the first diagram there shows only direction from external services (Storm, Azure Functions, etc.) towards ChangeFeed.

Is there any pattern that (is not obvious for me) we can use for getting notified about changes in Cosmos DB over ChangeFeed? Or there really needs to be support built-in eg Azure Functions to get this "PUSH" scenario working?

Thanks a lot

The Azure Cosmos DB Change Feed processor library was released recently in order to make listening to the Azure Cosmos DB change feed a bit easier. It handles automatic lease management for a set of change feed listening "workers" to read across Cosmos DB partitions; enabling you to easily scale out the set of change feed listening workers. Using this, you can build a pub/sub model on top of the consumer of the library.

With that said, integration between Azure Cosmos DB's change feed + Azure Functions is on the Azure Cosmos DB roadmap (as of current date - 8/5/17) to help make pushing changes even easier to accomplish. No specific dates have been announced for this yet; but do stay tuned.

Wanted to provide some updates to this question since the Azure Functions support has been added by now and the Change Feed Processor library has been fully integrated in the Azure SQL SDK for Java/.NET.

It is really easy to get notifications from a Change Feed, without using Azure Functions, through the usage of the ChangeFeedProcessor API - but bear in mind, it is not really a " PUSH " scenario as the Change Feed is queried in a polling fashion by the client!

The code would look like this:

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();
}

You can check out more in-depth details about the Azure CosmosDB Change Feed here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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