A Simple Azure Queuing and Functions Example

azure functions sql database azure queues example code

Today just wanted to highlight a simple Azure SQL and Azure Queue example that uses Azure Functions to schedule and move data to SQL Azure from a REST API in parallel. It shows a heavy use of bindings to keep the code very tight while being highly functional. My favorite part is the SchedulePokemonQueue function, in 11 lines of code it gets all of the data from the SQL DB and drops them onto the queue to be processed with essentially just some scaffolding and a for loop.

To cut to the chase, you can check out the code at BasicQueueExample @ GitHub.

I am using the PokeAPI for example data as it is both fun to use and interesting to look at! The end goal is to have a database with a set of API calls we want to make, schedule those calls, and then run each call in parallel. We handle that by using Azure Queue Storage to track what we need to run. We are using Queue Triggers in Azure Functions to run functions as items land in the queue.

Prerequisites

If you want to run this yourself you will need:

Test and Deploy

You will be able to quickly test and deploy this function following the instructions laid out as part of the Create your first C# function in Azure using Visual Studio quickstart

The most important items you will have to change is setting the STORAGE_ACCOUNT, DATA_LAKE, and SQL_AZURE_CONNECTION_STRING application settings to your respective resources.

If you are testing locally and would like to fire the timers without waiting for the triggers, you can make a post call to the admin endpoint. As an example you could use PowerShell to call the endpoint to run the GetAllPokemon function:

Invoke-WebRequest -Uri "http://localhost:7071/admin/functions/GetAllPokemon" -Method Post -Body "{}" -ContentType "application/json"

Resources

You can dig through the code comments to see where I use these different items specifically. These documents are how I build out the important items though and give you some end to end information and more depth so you can learn how to build these on your own.

Previous Post Next Post