I need to translate the C# code below into something that can be used by Azure Stream Analytics.
I've got a C# application that is similar to the following:
var inputEvents = new List<Event>();
foreach (var file in files){
(List<Event> events, DateTime maxDate) = ProcessEvents(file, inputEvents);
inputEvents = events.Where(e => e.Duration == null).ToList();
}
Where ProcessEvents() passes inputEvents to other helper methods
I need to implement this whole code using Stream Analytics. The var file part is implemented by gathering a bunch of events using Collect(). Each batch is sent to a UDF which acts as ProcessEvents(). However, ProcessEvents() returns other events that are needed for the next iteration. Since UDF is stateless, the next batch won't be able to use the returned events from the previous batch.
How do I rewrite the C# code above in Stream Analytics?
I've tried the following:
- Use UDA to store the returned events. Failed because for some reason it can't store a JSON array and constantly modify it.
- Use Reference Data input to store the returned events. Failed because they can only be used in Stream Analytics using a JOIN and not within a UDF.
Stream Analytics T-SQL Query:
WITH eventsCollection AS (SELECT COLLECT() AS allEvents
FROM EventHubStreamMessage
GROUP BY SessionWindow(minute,2,4)),
step1 AS (
SELECT UDF.SampleProcessEvents(allEvents) as Source
FROM eventsCollection
)
SELECT *
INTO [StorageTable]
FROM step1
Stream Analytics UDF Code (short version):
function main(allEvents){
allEvents = JSON.stringify(allEvents);
var inputEvents = new Array ();
return processEvents(JSON.parse(allEvents), inputEvents)
}
function processEvents(allEvents, inputEvents){
for (i=0l i<allEvents.length; i++){
if (allEvents[i].Event =="ON"){
powerOn(inputEvents);
}
}
}
function powerOn(inputEvents){
return true;
}