CosmosDB Change Feed with Azure Functions
By knowing what is changed in your database, you can trigger all kind of events and you can make your application work very smart. SQL Server has similar functionality but like many other features Log shipping is usually blocked by DBAs or the company policies. In CosmosDB, you don't need to do anything to enable Change Feed feature! It's already enabled, all you need to do is to configure it. Easiest way to catch change feed events is Azure Functions.
There are many ways to create Azure Functions, I am going to use the Azure portal to configure the change feed. First, we need to create an Azure Function, to do that, go to Compute and Pick Azure Function.
After that, we need to give some basic information about the function we are trying to create. Depending on your settings, you might need to create new resource group or Storage for this function. You can pick .Net, Javascript or Java as programming language. When you are ready, all you need to do is, click on Create and wait until your new function is ready to use.
When the Azure function is deployed and ready, you can go back to CosmosDB page and click on Azure Function link. In this point, your new function should be listed here.
You will see the following page after you click on Add Azure Function. Change Feed is going to create a new collection name leases and It is going to use this collection to track changes. The Azure Function I just created named CosmosTrigger is available in Azure Function app. You can name the function whatever you like.
Click Save to save your function. Next we are going to configure this function and pick the CosmosDB and collection information. To start click on Integrate button in the left side and give your database name and container name information to the function. Your new function might ask you to install CosmosDB reference to your function, you need to click Install. That's why you see a box with text Extension Installation Succeded in the following picture. When all is completed, click on Save to continue.
- Azure Queue Service
- Azure Blob Service
- HTTP
- Azure Service Bus
- Azure Table Storage
- Azure Event Hubs
- SendGrid
- Twilio SMS
- Excel Table
- Outlook Mail Message
- OneDrive File
- MS Graph WebHook
- Azure CosmosDB
Now we are ready to program this function. Click on your Azure function name and you should see a screen like this one. You can write any code you like in this box. As you can see Run function receives a list of documents from CosmosDB. These are the updated/new documents. In Run function, you can do what every you want with these documents. To test the function you can use the test document in the right side. When you click Run, application will send the document in the test window and you can test your function without updating CosmosDB documents. In the following example, I was on my way to push changed/new data to my SQL Server. In this way, I can analyze the data in my SQL Server.
No comments:
Post a Comment