A flex operator takes input from one or more streams/windows and produces a derived stream or window as its output. It allows the use of SPLASH code to specify customizable processing logic.
CREATE FLEX procedureName
IN input1 [KEEP keep_spec] [,...]
OUT [OUTPUT|LOCAL] [STREAM|DELTA STREAM|WINDOW] name schema_clause
[PRIMARY KEY (column1 [,...])] [store_clause] [keep_clause] [aging_clause]
[GUARANTEE DELIVERY]
BEGIN
[local-declare-block]
ON input1 { [statement1 [,...]] };
[EVERY interval { [statement1 [,...]] };]
[ON START TRANSACTION { [statement1 [,...]] };]
[ON END TRANSACTION { [statement1 [,...]] };]
END;
procedureName | The name of the Flex operator being created. |
IN input1 | Inputs to the Flex operator are declared in the IN clause. The inputs can be streams, delta streams, windows, or outputs of another flex operator. |
KEEP keep_spec | The KEEP clause modifies the retention policy of existing input elements that are either delta streams or windows. |
OUT output_element | The output of the Flex operator is defined in this clause. A Flex stream can have only one output. It can be a stream, a delta stream or a window, either local or output. |
name | The name of the output element must be included in the OUT clause. This name is used when running queries against the flex operator. |
schema_clause | This clause is mandatory for all output elements: stream, delta stream, and window. See SCHEMA Clause for more details. |
PRIMARY KEY (column1 [,...]) | (Optional) This clause may be used when specifying either a delta stream or a window as the output element. See PRIMARY KEY Clause for more details. |
store_clause | (Optional) This clause may only be used when specifying a window as the output element. See STORE Clause for more details. |
keep_clause | (Optional) This clause may only be used when specifying a window as the output element. See KEEP Clause for more details. |
aging_clause | (Optional) This clause may only be used when specifying a window as the output element. See AGING Clause for more details. |
GUARANTEE DELIVERY | (Optional) Enables guaranteed
delivery for this window. Any window with guaranteed delivery must also be assigned
to a log store.
Streams and delta streams do not support guaranteed delivery. They do support persistent subscribe pattern, which provides similar benefits. |
local-declare-block | (Optional) The DECLARE block can define variables and functions of all types, including complex data types such as records, vectors, dictionaries and event caches. |
ON input1 | The ON input clause must be declared for every input of the Flex operator. The SPLASH code specified in this block is executed each time an input record is received. If an input element does not require processing, use an empty ON input clause. |
EVERY interval | (Optional) The SPLASH statements specified in this block are executed every time the interval expires. The interval can be specified explicitly, or specified through an interval type parameter. |
ON START TRANSACTION | (Optional) The SPLASH statements specified in this block are executed at the start of each transaction. |
ON END TRANSACTION | (Optional) The SPLASH statements specified in this block are executed at the end of each transaction. |
statement1 | (Optional) SPLASH statement to specify the processing logic. |
The CREATE FLEX statement creates a Flex operator that accepts any number of input elements and produces one output element. The input elements are previously existing streams, delta streams, and windows defined in the project. If the input element is a delta stream or window, you can modify its retention policy by specifying a KEEP clause. The output element is a stream, delta stream, or window with an unique name generated by the Flex operator. Specification of the SCHEMA clause is mandatory for all output element types. Specification of the PRIMARY KEY is mandatory for output elements that are delta streams or windows.
When you enable guaranteed delivery on a window that has registered guaranteed delivery subscribers, the window stores a copy of every event it produces in its log store until all the registered guaranteed delivery subscribers acknowledge receiving the events.
Because copies of events are kept in the same log store the window is assigned to, the log store for a guaranteed delivery window must be significantly larger than the log store for a similar window without guaranteed delivery. Ensure that the log store for every guaranteed delivery window is large enough to accommodate the required events. If the log store runs out of room, the project server shuts down.
The ON input clause contains the processing logic for inputs arriving on a particular input element. Specification of the ON input clause is mandatory for each input of the Flex operator. The ON START TRANSACTION and ON END TRANSACTION clauses are optional and contain processing logic that should be executed at the start/end of each transaction respectively. The optional EVERY interval clause contains logic that is executed periodically based on a fixed time interval independent of any incoming events.
CREATE FLEX ComputeAveragePrice IN NASDAQ_Trades OUT OUTPUT WINDOW AverageTradePrice SCHEMA (Symbol string, AveragePrice money(4) ) PRIMARY KEY(Symbol) STORE store1 GUARANTEE DELIVERY BEGIN DECLARE typedef [|money(4) TotalPrice; integer NumOfTrades] totalRec_t; dictionary(string,totalRec_t) averageDictionary; END; ON NASDAQ_Trades { totalRec_t rec := averageDictionary[NASDAQ_Trades.Symbol]; if( isnull(rec) ) { averageDictionary[NASDAQ_Trades.Symbol] := [|TotalPrice = NASDAQ_Trades.Price; NumOfTrades = 1]; } else { // accumulate the total price and number of trades per input record averageDictionary[NASDAQ_Trades.Symbol] := [|TotalPrice=rec.TotalPrice + NASDAQ_Trades.Price; NumOfTrades=rec.NumOfTrades + 1]; } }; EVERY 5 SECONDS { totalRec_t rec; for (sym in averageDictionary ) { rec := averageDictionary[sym]; output setOpcode([Symbol=sym;|AveragePrice=(rec.TotalPrice/rec.NumOfTrades);], upsert); } }; END;