Introduction
For the last few days, I have been playing with my MXChip Azure IoT Dev Kit. In this article, we are going to see how we can set up an Azure Function as an Output job topology of an Azure Stream Analytics job. Doesn’t that sound interesting? In our previous articles, we have already seen what an Azure Stream Analytics Job is and how can we create it by using the portal and Visual Studio. If you haven’t read those articles, I strongly recommend you to read them. Let’s jump into this article now.
You can always read this article on my blog here.
Background
As I mentioned earlier, in this article we will be,
- Using our existing Azure Stream Analytics job.
- Creating a new Azure Function App.
- Setting up the newly created Azure function as an output job topology of the stream analytics job.
- Monitoring the data coming to the Azure Function from the stream analytics job.
Play with Azure Functions
Yeah, we are going to play with it. Let’s go and create one then.
Creating an Azure Function
To create an Azure Function application, you need to log into your Azure portal and click on the "Create a resource" icon, and then, you can search for the ” Function App”.
In the next screen, provide the following information.
- App Name
- Subscription
- Resource Group
- OS
- Hosting plan
- Location
- Runtime stack
- Storage
- Application Insights
Here, the Consumption Plan Hosting plan allows you to pay per execution, and the App Service plan allows you to have a predefined capacity. For the runtime stack, we will use .NET, however, you are free to use anything you wish.
Once you have created the same, you should be able to see it under the Function Apps section.
Creating an Azure Function Solution and Function
Now, let’s go to our Visual Studio and create a new solution for our Azure Function.
Azure Function Project Type
Now, you can right-click on your newly created project and add a new HttpTrigger Function. We will keep the access rights to anonymous for now. I have named my function as “GetData”. For now, let’s just get the data from our Stream Analytics job and just check the length.
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Extensions.Http;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace ml.IoTPlatform.AzureFunctions {
- public static class GetData {
- [FunctionName("GetData")] public static async Task < HttpResponseMessage > Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestMessage req, ILogger log) {
- log.LogInformation($ "GetData function triggered with Uri {req.RequestUri}");
- string content = await req.Content.ReadAsStringAsync();
- log.LogInformation($ "String content is {content}");
- dynamic data = JsonConvert.DeserializeObject(content);
- log.LogInformation($ "Data count is {data?.Count}");
- if (data ? .ToString() ? .Length > 262144) {
- return new HttpResponseMessage(HttpStatusCode.RequestEntityTooLarge);
- }
- return req.CreateResponse(HttpStatusCode.OK, "Success");
- }
- }
- }
As you can see, we are not doing anything much for now; we are just receiving the data as HttpRequestMessage and reading the content as req.Content.ReadAsStringAsync() and then deserializing the object. If you are not doing this step, you may get an error as “No MediaTypeFormatter is available to read an object of type ‘Object’ from content with media type ‘application/octet-stream’.“
We are also checking the entity length and if it is too large, we are sending a HttpResponseMessage with status code 413.
Publish the Azure Function App
To publish your Azure Function app, just right-click on your project and click "Publish". Then, set up your publishing target by choosing the existing Azure Function App. Remember, we have created one earlier? Once you publish the same, you can go into your Function app and see your Function. You can also test the same with some dummy data.
There are probabilities to get an error as “Web Deploy cannot modify the file on the destination because it is locked by an external process” when you try to publish your Function App from Visual Studio, while your Function App is running. To fix this, you can see my answer here.
Function App in Portal
Azure Stream Analytics Job
Let’s go back to our Azure Stream Analytics as we have already configured our Azure Function App successfully.
Configure Azure Function Output

Divyesh PadamaniPosted Dec 24, 2018, 1:03 AM
Nice explaination....