CREATE STREAM Statement

Create either an input stream that receives events from external sources, or a derived stream of events that is the result of a continuous query applied to one or more inputs.

Syntax

CREATE INPUT STREAM name schema_clause
[filter-expression-clause]
[ autogenerate_clause ]
;

CREATE [ LOCAL | OUTPUT ] STREAM name [ schema_clause ]
 [ local-declare-block ]
[partition_clause] 
as_clause
;

Components

schema_clause Specifies the schema. The schema clause is required for input streams, but is optional for local and output streams. If the schema is not specified for local and output streams, it is deduced automatically by the compiler based on the query specification.
partition_clause

(Optional) Creates a specified number of parallel instances of the stream. The partition type specifies how to partition the input for the stream. See the PARTITION BY Clause for more information.

filter-expression-clause (Optional) Can be specified on an input stream. This clause filters the events before accepting them from the adapter or an outside publisher. In the expression, reference column values in the form stream.column, where stream is the name of the stream being created by this statement, and column is the name of the column being referenced.
autogenerate_clause

(Optional) This can be used to automatically add a sequence number to each event. One or more columns are specified (datatype long) and the value in the column is incremented for each incoming event. Valid only for input streams. See AUTOGENERATE Clause for more information.

local-declare-block Allows variable and function declarations that can be accessed in expressions in the query. You cannot define a local-declare-block on an input stream.
as_clause For derived streams, this contains the continuous query (SELECT clause, FROM clause) that will define the output of this stream.

Usage

The CREATE STREAM statement explicitly creates a stateless element known as a stream, which can be designated as input, output, or local. Input streams include a mandatory schema, and may include an optional filter expression that can remove unneeded data before further processing. Each incoming event is processed, any output is published, and then the stream is ready to process the next event.

Output and local streams have an optional schema. They can contain a local declare block to define variables and functions that can be used in the SELECT clause of the query.

Example

This creates an input stream with a filter:

CREATE INPUT STREAM InStr
SCHEMA (Col1 INTEGER, Col2 STRING)
WHERE InStr.Col2='abcd';

This creates an output stream where the schema is implicitly determined by the SELECT clause:

CREATE OUTPUT STREAM OutStr as
SELECT InStr.Col1, InStr.Col2 
FROM InStr 
WHERE InStr.Col1 > 1000;

The following statement creates an input stream with auto generated values beginning at 100000 for the TradeId column, filtering out trades with prices below 1000. Note that the filtering is done after the TradeId is generated.

CREATE INPUT STREAM BigTrades
SCHEMA (TradeId long, Symbol string, Shares integer, Price money(4))
WHERE BigTrades.Price > 1000
AUTOGENERATE (TradeId) FROM 1000000;