<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ASA &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/asa/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Fri, 11 Jan 2019 09:41:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>ASA &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Azure Function as Output Job Topology of an Azure Stream Analytics Job</title>
		<link>https://mail.sibeeshpassion.com/azure-function-as-output-job-topology-of-an-azure-stream-analytics-job/</link>
					<comments>https://mail.sibeeshpassion.com/azure-function-as-output-job-topology-of-an-azure-stream-analytics-job/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 19 Dec 2018 14:37:18 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[ASA]]></category>
		<category><![CDATA[Azure Function]]></category>
		<category><![CDATA[Azure Function and Azure Stream Analytics]]></category>
		<category><![CDATA[Azure IoT]]></category>
		<category><![CDATA[IoT Hub]]></category>
		<category><![CDATA[MXChip]]></category>
		<category><![CDATA[Stream Analytics Job]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13479</guid>

					<description><![CDATA[[toc] Introduction For the last few days, I am playing with my Azure IoT Dev Kit MXChip. 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. Isn&#8217;t that sound interesting? In our previous articles, we have already seen what is an Azure Stream Analytics Job and How can we create in by using the portal and Visual Studio. If you haven&#8217;t read those articles, I strongly recommend you to read. Let&#8217;s jump on to this article now. Background As I have mentioned [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>[toc]</p>



<h2 class="wp-block-heading">Introduction</h2>



<p>For the last few days, I am playing with my Azure IoT Dev Kit MXChip. 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. Isn&#8217;t that sound interesting? In our previous <a href="https://sibeeshpassion.com/tag/stream-analytics-job/">articles</a>, we have already seen what is an Azure Stream Analytics Job and How can we create in by using the portal and Visual Studio. If you haven&#8217;t read those articles, I strongly recommend you to read. Let&#8217;s jump on to this article now.</p>



<h2 class="wp-block-heading">Background</h2>



<p>As I have mentioned earlier, in this article we will be,&nbsp;</p>



<ol class="wp-block-list"><li>using our existing Azure Stream Analytics job.</li><li>creating a new Azure Function App.</li><li>setting up the newly created Azure function as an output job topology of the stream analytics job.</li><li>monitoring the data coming to the Azure Function from the stream analytics job.</li></ol>



<h2 class="wp-block-heading">Play with Azure Function</h2>



<p>Yeah, we are going to play with it. Let&#8217;s go and create one then.</p>



<h3 class="wp-block-heading">Creating an Azure Function</h3>



<p>To create an Azure Function application, you need to login to your Azure portal and click on the Create a resource icon, and then you can search for the &#8221; Function App&#8221;.&nbsp;</p>



<p>In the next screen, provide the following information.&nbsp;</p>



<ol class="wp-block-list"><li>App Name</li><li>Subscription</li><li>Resource Group</li><li>OS</li><li>Hosting plan</li><li>Location&nbsp;</li><li>Runtime stack</li><li>Storage</li><li>Application Insights</li></ol>



<p>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.&nbsp;</p>



<p>Once you have created the same, you should be able to see it under the Function Apps section.&nbsp;</p>



<h3 class="wp-block-heading">Creating an Azure Function Solution and Function</h3>



<p>Now let&#8217;s go to our Visual Studio and create a new solution for our Azure Function.&nbsp;</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img fetchpriority="high" decoding="async" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Azure-Function-Project-Type-1024x419.jpg" alt="" class="wp-image-13480" width="512" height="210" srcset="/wp-content/uploads/2018/12/Azure-Function-Project-Type-1024x419.jpg 1024w, /wp-content/uploads/2018/12/Azure-Function-Project-Type-300x123.jpg 300w, /wp-content/uploads/2018/12/Azure-Function-Project-Type-768x314.jpg 768w, /wp-content/uploads/2018/12/Azure-Function-Project-Type-400x163.jpg 400w, /wp-content/uploads/2018/12/Azure-Function-Project-Type.jpg 1375w" sizes="(max-width: 512px) 100vw, 512px" /><figcaption>Azure Function Project Type</figcaption></figure></div>



<p>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 &#8220;GetData&#8221;. For now, let&#8217;s just get the data from our Stream Analytics job and just check the length.&nbsp;</p>



<pre class="wp-block-code"><code>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&lt;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");
        }
    }
}</code></pre>



<p>As you can see we are not doing nothing much for now, we are just receiving the data as&nbsp;HttpRequestMessage and we are reading the content as&nbsp;req.Content.ReadAsStringAsync() and then deserialize the object. If you are not doing this step, you may get an error as&nbsp;&#8220;<strong>No MediaTypeFormatter is available to read an object of type &#8216;Object&#8217; from content with media type &#8216;application/octet-stream&#8217;.</strong>&#8220;</p>



<p>We are also checking the entity length, and if it is too large we are sending a&nbsp;HttpResponseMessage with status code 413.&nbsp;</p>



<h3 class="wp-block-heading">Publish the Azure Function App</h3>



<p>To publish your Azure Function app, just right click on your project and click Publish and then set up your publish target by choosing the existing Azure Function App, remember we have created on 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.&nbsp;</p>



<p>There are probabilities to get an error as &#8220;<strong>Web Deploy cannot modify the file on the destination because it is locked by an external process</strong>&#8221; 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 <a href="https://stackoverflow.com/questions/37918650/azure-web-app-deploy-web-deploy-cannot-modify-the-file-on-the-destination-becau/53833766#53833766">here</a>.&nbsp;</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="651" height="284" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Function-App-in-Portal.jpg" alt="" class="wp-image-13481" srcset="/wp-content/uploads/2018/12/Function-App-in-Portal.jpg 651w, /wp-content/uploads/2018/12/Function-App-in-Portal-300x131.jpg 300w, /wp-content/uploads/2018/12/Function-App-in-Portal-400x175.jpg 400w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>Function App in Portal</figcaption></figure></div>



<h2 class="wp-block-heading">Azure Stream Analytics Job</h2>



<p>Let&#8217;s go back to our Azure Stream Analytics now as we have already configured our Azure Function App successfully.&nbsp;</p>



<h3 class="wp-block-heading">Configure Azure Function Output</h3>



<p>In my previous <a href="https://sibeeshpassion.com/azure-stream-analytics-job-and-tools-for-visual-studio/">article</a>, we had created an Azure Stream Analytics job solution using Visual Studio, let&#8217;s open that solution now and configure the new output for Azure Function.&nbsp;</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="640" height="535" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Solution-Explorer.jpg" alt="" class="wp-image-13482" srcset="/wp-content/uploads/2018/12/Solution-Explorer.jpg 640w, /wp-content/uploads/2018/12/Solution-Explorer-300x251.jpg 300w, /wp-content/uploads/2018/12/Solution-Explorer-400x334.jpg 400w" sizes="(max-width: 640px) 100vw, 640px" /><figcaption>Solution Explorer</figcaption></figure></div>



<p>While configuring the Azure Function Output, please make sure that you are selecting the existing azure function app.</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="651" height="281" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Azure-Function-Output-Configuration.jpg" alt="" class="wp-image-13483" srcset="/wp-content/uploads/2018/12/Azure-Function-Output-Configuration.jpg 651w, /wp-content/uploads/2018/12/Azure-Function-Output-Configuration-300x129.jpg 300w, /wp-content/uploads/2018/12/Azure-Function-Output-Configuration-400x173.jpg 400w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>Azure Function Output Configuration</figcaption></figure></div>



<h3 class="wp-block-heading">Update the Script</h3>



<p>We should also do some changes in our Script.asaql file to support our newly created output.&nbsp;</p>



<pre class="wp-block-code"><code>WITH BasicOutput AS 
(
SELECT    
    messageId,
    deviceId,
    temperature,
    humidity,
    pressure,
    pointInfo,
    IoTHub,
    MAX(EventEnqueuedUtcTime) AS EventEnqueuedUtcTime,
    EventProcessedUtcTime,
    PartitionId    
FROM
    Input TIMESTAMP By EventEnqueuedUtcTime
    GROUP BY TUMBLINGWINDOW(second, 10), 
    messageId, 
    deviceId,
    temperature,
    humidity,
    pressure,
    pointInfo,
    IoTHub,
    EventEnqueuedUtcTime,
    EventProcessedUtcTime,
    PartitionId
)

SELECT * INTO SQLServerOutput FROM BasicOutput
SELECT * INTO AzureFunctionOutput FROM BasicOutput</code></pre>



<h3 class="wp-block-heading">Updating the TLS Version</h3>



<p>Once that is done, just click the button Submit to Azure, if you have any doubts in this section, read my previous posts on this topic. Now let&#8217;s log in to the portal again and see all the outputs, inputs, and the query is been published or not.</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="651" height="220" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Outputs-in-Portal.jpg" alt="" class="wp-image-13484" srcset="/wp-content/uploads/2018/12/Outputs-in-Portal.jpg 651w, /wp-content/uploads/2018/12/Outputs-in-Portal-300x101.jpg 300w, /wp-content/uploads/2018/12/Outputs-in-Portal-400x135.jpg 400w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>Outputs in Portal</figcaption></figure></div>



<p>Cool!. Well done, it seems like it is published. Now if you click on the AzureFunctionOutput, you may get a warning as &#8220;<strong>Please make sure that the Minimum TLS version is set to 1.0 on your Azure Functions before you start your ASA job</strong>&#8220;. I would rather treat this as an error instead of a warning because without making this changes our Azure Stream Analytics job will not write to our Azure Function. So this is very important, I spent many hours in this and finally found this was the root cause of my issue, you can see my answer about this <a href="https://stackoverflow.com/questions/53851553/an-error-occurred-send-events-azure-function-output-adapter-failed-to-write-eve/53851554#53851554">here</a>.</p>



<p>So just go to your Azure Function App and click on Platform Features -&gt; SSL -&gt; Minimum TLS Version</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="511" height="397" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Setting-TLS-Version.jpg" alt="" class="wp-image-13485" srcset="/wp-content/uploads/2018/12/Setting-TLS-Version.jpg 511w, /wp-content/uploads/2018/12/Setting-TLS-Version-300x233.jpg 300w, /wp-content/uploads/2018/12/Setting-TLS-Version-400x311.jpg 400w" sizes="(max-width: 511px) 100vw, 511px" /><figcaption>Setting TLS Version</figcaption></figure></div>



<p>There is a saying that developers don&#8217;t care about warning but only the errors, in some cases it is true. Hm, I was just kidding.&nbsp;</p>



<h2 class="wp-block-heading">Output</h2>



<p>Once you are done everything mentioned, you are good to go and start your Stream Analytics job, please make sure that your MXChip is connected to a power source so that the device can start sending the data.&nbsp;</p>



<h3 class="wp-block-heading">Checking the SQL Server Output</h3>



<p>Now let&#8217;s login to our SQL Server Database and run the below query to make sure that we are getting the data from the device.</p>



<pre class="wp-block-code"><code>SELECT TOP (1000) [Id]
      ,[messageId]
      ,[deviceId]
      ,[temperature]
      ,[humidity]
      ,[pressure]
      ,[pointInfo]
      ,[IoTHub]
      ,[EventEnqueuedUtcTime]
      ,[EventProcessedUtcTime]
      ,[PartitionId]
  FROM [dbo].[StreamData] order by [EventEnqueuedUtcTime] desc</code></pre>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="651" height="286" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/SQL-Server-Output-Data.jpg" alt="" class="wp-image-13486" srcset="/wp-content/uploads/2018/12/SQL-Server-Output-Data.jpg 651w, /wp-content/uploads/2018/12/SQL-Server-Output-Data-300x132.jpg 300w, /wp-content/uploads/2018/12/SQL-Server-Output-Data-400x176.jpg 400w" sizes="(max-width: 651px) 100vw, 651px" /><figcaption>SQL Server Output Data<br></figcaption></figure></div>



<h3 class="wp-block-heading">Checking Azure Function Output&nbsp;</h3>



<p>To check the Azure Function Output, we can go back to our Azure Function and click on the Function and use the <strong>Monitor </strong>option.</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" width="1024" height="254" src="https://sibeeshpassion.com/wp-content/uploads/2018/12/Azure-Function-Output-Data-1024x254.jpg" alt="" class="wp-image-13487" srcset="/wp-content/uploads/2018/12/Azure-Function-Output-Data-1024x254.jpg 1024w, /wp-content/uploads/2018/12/Azure-Function-Output-Data-300x74.jpg 300w, /wp-content/uploads/2018/12/Azure-Function-Output-Data-768x190.jpg 768w, /wp-content/uploads/2018/12/Azure-Function-Output-Data-400x99.jpg 400w, /wp-content/uploads/2018/12/Azure-Function-Output-Data.jpg 1574w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Azure Function Output Data</figcaption></figure></div>



<p>Please be noted that you can always check your Azure Stream Analytics job Activity Log if you found something is not working.&nbsp;</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>In this article, we have learned how to,</p>



<ol class="wp-block-list"><li>work with an Azure Stream Analytics job</li><li>create an Azure Function App</li><li>create Azure Function App solution in Visual Studio</li><li>write an HttpTrigger&nbsp;function and publish the same to the Azure Function App</li><li>set up the Azure Function App as an output job topology of Azure Stream Analytics job</li><li>Use the created package in another solution</li></ol>



<p>In our next article, we will see how you can send this Azure Function Output data to an Azure SignalR service and then get the same data in an Angular Application. I can&#8217;t wait to write my next article.&nbsp;</p>



<h2 class="wp-block-heading">Your turn. What do you think?</h2>



<p>Thanks a lot for reading. I will come back with another post on the same topic very soon.&nbsp;Did I miss anything that you may think which is needed? Could you find this post as useful? Kindly do not forget to share me your feedback.</p>



<p>You can always see my IoT articles <a href="https://sibeeshpassion.com/category/iot/">here</a>.&nbsp;</p>



<p>Kindest Regards<br>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/azure-function-as-output-job-topology-of-an-azure-stream-analytics-job/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
