What is CosmosDB Change Feed?
From Microsoft documentation: Change feed in Azure Cosmos DB is a persistent record of changes to a container in the order they occur. Change feed support in Azure Cosmos DB works by listening to an Azure Cosmos container for any changes. It then outputs the sorted list of documents that were changed in the order in which they were modified. The persisted changes can be processed asynchronously and incrementally, and the output can be distributed across one or more consumers for parallel processing.
Watch the demo video
Here I have used Azure Function to listen to the change feed and below is the Azure Function code
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Threading.Tasks;
namespace Company.Function
{
public static class CosmosDBTriggerOrd
{
[FunctionName("CosmosDBTriggerOrd")]
public static async Task Run([CosmosDBTrigger(
databaseName: "adventureworks",
collectionName: "Product",
ConnectionStringSetting = "cosmos_DOCUMENTDB",
CreateLeaseCollectionIfNotExists = true,
LeaseCollectionName = "reportingCollection")]IReadOnlyList<Document> input, ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].Id);
var listTasks = new List<Task>();
foreach (var doc in input)
{
var jsonString = doc.ToString();
log.LogInformation(jsonString);
}
await Task.WhenAll(listTasks);
}
}
}
}
Top comments (0)