CREATE FLEX Statement

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.

Note: The name of the Flex operator exists only for labeling in Studio and cannot be referred to in queries. Instead, refer to the output element.

Syntax

CREATE FLEX procedureName
 IN input1 [KEEP keep_spec], ...
	OUT output_element
		BEGIN
			[DECLARE 
				//variable and function declarations
			END;]
			ON input1 {
				//statements
				};
				[EVERY interval{
				 //periodically executing tasks
				};]
				[ON START TRANSACTION {
					//tasks to be executed
					//at the start of every transaction
				};]
				[ON END TRANSACTION {
					//tasks to be executed
					//at the end of each transaction
				};]
		END;

OUT output_element
output_element:
{{[OUTPUT/LOCAL] STREAM name schema_clause
		[OUTPUT/LOCAL] DELTA STREAM name schema_clause|PRIMARY KEY{column1,column2,...)
		[OUTPUT/LOCAL] WINDOW name schema_clause}
		[PRIMARY KEY(column1,column2,...)][store_clause][keep_clause][aging_clause]
	}
}

Components

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 the OUT clause. A Flex stream can have only one output. The SCHEMA clause is mandatory for all output types.
DECLARE ... END; (Optional) The DECLARE block can define variables and functions of all types, including complex data types such as records, vectors, dictionaries and event caches.

See the SPLASH Programmers Guide for additional information.

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 EVERY interval clause allows you to specify a block of code that is executed every time the interval expires. The interval can be specified explicitly, or specified through an interval type parameter.
ON START TRANSACTION and ON END TRANSACTION (Optional) The SPLASH statements specified in the START/END transaction block are executed at the start/end of each transaction respectively. You can individually specify a START TRANSACTION block or END TRANSACTION block, without the other block.

Usage

The CREATE FLEX statement is used to create 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, its retention policy can be modified 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.

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.

Restrictions

  • A KEEP clause can be specified for the input of a Flex operator if the input element is a window or a delta stream.
  • You cannot declare functions in the ON input and EVERY clauses.
  • You can define event cache types only in the local DECLARE block associated with the statement.
  • A Flex delta stream (a Flex stream for which the output is a delta stream) cannot be used to generate records with update or upsert opcodes. To generate records with these opcodes, use a Flex window instead of a Flex delta stream.
  • The SPLASH output statement can be used inside the body of a function defined only in the local declare block of a Flex operator and not in a global declare block or a local declare block of any other element.

Example

This example computes the average trade price every five seconds.

CREATE FLEX ComputeAveragePrice
 IN NASDAQ_Trades
 OUT OUTPUT WINDOW AverageTradePrice SCHEMA (Symbol string, 
 AveragePrice money(4) ) PRIMARY KEY(Symbol)
 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;