Azure Function Apps – reading events from ServiceBus and writing to Redis Cache
This blog is part of IoT Series, where I am trying to build few IoT devices that push events to Azure EventHub. From the EventHub, Azure Stream Analytics will execute my query to calculate average values for each individual device and publish these average values to Azure ServiceBus. From Azure ServiceBus, I am going to read the average values in Azure Functions Apps and save them into Azure Redis Cache. My Azure Website will poll this Redis Cache and displays the average values.
Here are list of blog posts in this series:
-
- Azure IoT
- Azure EventHub–sending IoT device events to EventHub
- Azure ServiceBus Queue–reading query results from Stream Analytics
- Azure Stream Analytics–reading events from EventHub, running query and saving results to ServiceBus
- Azure Function Apps – reading events from ServiceBus and writing to Redis Cache
In this blog, I am going to show how to configure Azure Function Apps to read from Azure ServiceBus and write them to Azure Redis Cache.
-
Log into Azure Portal
Click on + New button
In the Search, type Function App
Select Function App and provide name, resource group, storage account as shown below
Click on Create button
Once the Function App is deployed, browse to newly created Function App
In the Function App, click on the + New Function
In the templates, select ServiceBusQueueTrigger – C# as shown below
Now we need to add ServiceBus Connection String
Click on new link next to the Service Bus Connection as shown below
Provide the connection string name and connection string to Azure Service Bus. Click here to learn how to get connection string.
Enter below code in the run.csx file
using System; using System.Threading.Tasks; using StackExchange.Redis; using Newtonsoft.Json; public static void Run(string myQueueItem, TraceWriter log) { log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}"); var msg = JsonConvert.DeserializeObject<MyOutput>(myQueueItem); if (msg == null) { log.Info("failed to convert msg to MyOutput"); return; } IDatabase cache = Connection.GetDatabase(); cache.StringSet(msg.devicename, myQueueItem); } private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => { return ConnectionMultiplexer.Connect("redis connection string"); }); public static ConnectionMultiplexer Connection { get { return lazyConnection.Value; } } class MyOutput { public string devicename { get; set; } public double avgtemp { get; set; } public double avgspeed { get; set; } }
Now we need to add NuGet package for Newtonsoft and Redis Cache
Click on the View files and click + to add a new file
Set the name of the new file as Project.json and add these lines
{ "frameworks": { "net46": { "dependencies": { "StackExchange.Redis":"1.1.603", "Newtonsoft.Json": "9.0.1" } } } }
Now try to run the Function, click on the Run button in the Run section as shown below
Check for any compiler errors in the above Logs section
next, push few events into EventHub using the console app, code is here
These events will travel from EventHub to Stream Analytics to Service Bus Queue to Function App to Redis Cache
To verify if the event reached Redis Cache, in the Azure Portal, navigate to Redis Cache and click on Console and execute these commands as shown