Apama 10.15.4 | Developing Apama Applications | Developing Apama Applications in EPL | Working with Streams and Stream Queries | Defining stream queries | Specifying input streams in from clauses
 
Specifying input streams in from clauses
In a stream query, each from clause specifies a stream that the query is operating on. The syntax of the from clause is as follows:
from itemIdentifier in streamExpr [WindowDefinition]
Syntax Element
Description
itemIdentifier
Specify an identifier that you want to use to represent the current item in the stream you are querying. You use this identifier in subsequent clauses in the query. For details about the characters you can specify, see Identifiers.
The type of the identifier is the same as the type of the items that are in the stream you are querying.
There is no link between an item identifier in a query and a variable that you might define elsewhere in your code. In other words, it is okay for an in-scope variable to have the same name as an item identifier in a query. Inside the query, the item identifier hides that variable. See the second example below.
streamExpr
Specify an expression that returns a stream type. This is the stream that you want to query.
WindowDefinition
Define which portion of the stream to query. See Adding window definitions to from and join clauses.
Examples
The query below generates a stream of float items. The item identifier is a. The stream variable, ticks, refers to a stream of Tick events. The select clause specifies that each query result item contains only the price value from the Tick event. Details about the select clause are in Generating query results.
stream<float> prices := from a in ticks select a.price;
The all keyword followed by an event template is an expression of type stream referred to as a stream source template. Consequently, you can use this in a from clause. For example, you can modify the previous example to use the stream source template directly within the stream query:
stream<float> prices :=
   from a in all Tick(symbol="APMA") select a.price;
Notes
A stream query is an expression of type stream and so anywhere that you can specify a stream expression you can use a stream query in its place. (There is one exception to this. See Linking stream queries together.) This means you can nest stream queries to create a compound stream query. For example, consider the following non-nested stream queries:
stream<A> sA := all A();
 
stream<integer> derived :=
   from a in sA retain 2 select mean(a.x);
 
stream<B> sB :=
   from a in derived within 10.0 select B(stddev(a));
An equivalent way to write this is as follows:
stream<B> sB :=
   from b in
      from a in all A() retain 2 select mean(a.x)
   within 10.0
   select B(stddev(b));
The compiler generates the same stream network in both cases, so the performance is exactly the same. However, nesting stream queries beyond one level can make the compound stream query hard to understand.
To define a query that operates on two streams, specify two consecutive from clauses or specify a from clause followed by a join clause. See Joining two streams.