Search code examples
azureazure-functionsazure-stream-analytics

Stream Analytics Egress to Azure Functions


Microsoft announced support for sending data from Azure Stream Analytics to Azure Functions few days back:

https://azure.microsoft.com/en-us/blog/new-in-stream-analytics-output-to-azure-functions-built-in-anomaly-detection-etc/

I tried this but couldn't send data to Azure Functions. Is there any guide how to send data packet from IoT-hub -> Azure Stream Analytics -> Azure Functions?

The output is fine to other sources. This is the query I have:

WITH rpidata AS
(
SELECT 
*, 
DATEADD(Hour, 3, timecreated) AS FITimezone
FROM [rpi]
)
SELECT *
INTO [PowerBI]
FROM rpidata
SELECT *
INTO [storageout]
FROM rpidata
SELECT *
INTO [fnout]
FROM rpidata

The error message I get is:

Could not successfully send an empty batch to the Azure Function. Please make sure your function app name, function name, and API key are correct and that your Azure Function compiles. If all of those parameters are correct, Azure Function may be temporarily available at this time. Please try again later. Azure function returned with response code of 500: InternalServerError. It should respond with a 200, 202, or 204.

However the function is there, is running and is found automatically when I try to create the connection.

What kind of Function input I should use to receive the data? I n the example I linked function name is httptriggercsharp... Does streamjob send the data as json?


Solution

  • Not sure if you still need this, but for future reference: The ASA job will output the data in a JSON array to your Azure function. An example ASA query like this

    SELECT
       w.col1 as key1,
       w.col2 as key2,
       System.Timestamp as time
    INTO
       azfunction
    FROM
       [input] w;
    

    will arrive in your Function like this

    [
             {
                 "key1":"value1",
                 "key2":"value2",
                 "time":"2017-09-04T17:51:02.7986986Z"
             },
             {
                 "key1":"value3",
                 "key2":"value4",
                 "time":"2017-09-04T17:51:02.7986986Z"
             }
    ]
    

    How many elements the JSON array will contain, depends on how you set up the Az Function output in ASA as well as on how fast events arrive in ASA. The array might only have one element or 100 depending on your scenario.