I’d briefly played around with user-defined aggregates in StreamInsight with CTP3 but when I started working with the new Count Windows, I found I had to have one working. I learned a few things along the way that I hope will help someone.
The first thing you have to do is define a class:
public class IntegerAverage: CepAggregate<int, int>
{
public override int GenerateOutput(IEnumerable<int> eventData)
{
if (eventData.Count() == 0)
{
return 0;
}
else
{
return eventData.Sum() / eventData.Count();
}
}
}
In this case, I’ve defined an IntegerAverage class that inherits from CepAggregate. CepAggregate is then declared as a generic type taking an int input parameter and providing an int output. The GenerateOutput method is overriden and called when it’s time to process the input. In this case, I’ve just checked if there are any values, returned zero if not and returned an integer average if there are values.
2010-05-08