Export (0) Print
Expand All

Projection

The examples in this topic show how to perform specific computations over individual events as part of their transformation to output events. Such computations are performed by using projection. A projection operates on a CepStream<T1> stream and yields a new CepStream<T2> stream where CepStream<T> is stream of Event with payload of type T. Projections always occur when using the select clause in LINQ. They can create a new type implicitly or refer to a defined event type explicitly, as shown below.

In the following example, every event in the stream TestEventStream will be projected into a new event, according to the project expressions defined in the query. In the query, the fields i and f are multiplied by 2. These values are projected into the new events. The new event type here is an anonymous type, implicitly defined through the field assignments created by the project expression.

// Assuming the following input event type:
public class MyPayload
{
    public int i;
    public float f;
}

var queryProject = from c in TestEventStream
                   select new {i = c.i * 2, f = c.f * 2};

In the following example, every event in the input stream of type MeterReading will be projected into a new event in the stream realValueStream, using the explicitly declared event type MeterWattage. The consumption field for each event in the InputStream stream is converted to a double CLR type and divided by 10 and then assigned to the wattage field of the type MeterWattage.

public class MeterReading
{
    public int consumption;
}

public class MeterWattage
{
    public double wattage;
}

[…]

var realValueStream = from e in InputStream
                      select new MeterWattage {wattage = (double)e.consumption / 10};

The project expression cannot use a type's constructor. It must always specify the field assignment to the new (implicit or explicit) type through a MemberInit expression (the assignment of event fields within curly braces). However, this syntax is not required when projecting a single field of a primitive type, as shown in the following example:

public class MeterReading
{
    public int consumption;
}

public class MeterWattage
{
    public double wattage;
}

[…]

var realValueStream = from e in InputStream
                      select (double)e.consumption / 10;

Note that this projection implicitly produces a CepStream of primitive type double.

When an explicit type is used, each of its fields or properties must be initialized in the project expression.

Show:
© 2014 Microsoft