Search code examples
azureazure-stream-analytics

Stream Analytics - Passing multiple, individual records within a window into a UDF


I want to pass multiple, individual records within a set window (can be tumbling, hopping, sliding) without any aggregation into a javascript UDF like so:

Input data is:

{ "device":"A", "temp":20.0, "humidity":0.9, "param1": 83}
{ "device":"A", "temp":22.0, "humidity":0.9, "param1": 63}
{ "device":"B", "temp":15.0, "humidity":0.5, "param1": 13}
{ "device":"A", "temp":22.0, "humidity":0.5, "param1": 88}
{ "device":"A", "temp":22.0, "humidity":0.5, "param1": 88}

Pass records within a specified window as an object array:

function process_records(record_array) {
   //access individual records
   record_one_device = records[0].device
   record_two_device = records[1].device
   record_three_device = records[2].device
   ...
}

Thanks for any help!


Solution

  • According to your requirement, I assumed that you could leverage the Collect aggregate function from Azure Stream Analytics. Here is my test, you could refer to it:

    Input

    [  
        {
            "Make": "Honda",
            "Time": "2015-01-01T00:00:01.0000000Z",
            "Weight": 1000
        }, 
        {
            "Make": "Honda",
            "Time": "2015-01-01T00:00:03.0000000Z",
            "Weight": 3000
        },
        {
            "Make": "Honda",
            "Time": "2015-01-01T00:00:12.0000000Z",
            "Weight": 2000
        }, 
        {
            "Make": "Honda",
            "Time": "2015-01-01T00:00:52.0000000Z",
            "Weight": 1000
        }
    ]
    

    With the following query, I could retrieve the data contained in temporal windows as follows:

    SELECT
        Make,
        System.TimeStamp AS Time,
        Collect() AS records
    FROM
        Input TIMESTAMP BY Time
    GROUP BY
        Make,
        HoppingWindow(second, 10,10)
    

    enter image description here

    Then, you could call UDF.processRecords(Collect()) in your query. For more details, you could refer to common Stream Analytics usage patterns and Azure Stream Analytics UDF and Stream Analytics Window functions.