Perform calculations or comparisons across rows. Replace the CCL statements that filter rows with statements that create a window.
A Sybase CEP window maintains rows in memory to allow you to perform calculations or comparisons across those rows.
In this activity, you will be modifying the StockFilter project you created in the Filtering Data tutorial:
-- Create a window CREATE WINDOW FilteredTradesWindow SCHEMA (Symbol STRING, Price FLOAT, Volume INTEGER) KEEP 3 ROWS; -- Insert rows into the window INSERT INTO FilteredTradesWindow SELECT * FROM StockTrades WHERE Symbol = 'EBAY';
Note: You can also copy the text from the file MaintainingStateCCL.txt in the directory SybaseC8Repository/version/examples/GettingStarted/CCLFiles (where version is the version number of Sybase CEP Engine), between the lines "-- Maintaining State Code Block 1" and "-- Maintaining State Code Block 2."
The following points describe the CCL code:
The first statement creates a window named FilteredTradesWindow with a schema identical to that of the StockTrades input stream. Note that you can define a schema directly in CCL, not just through the Sybase CEP user interface as you did for the data streams. The KEEP clause specifies that this window maintain three rows. This code block is a single CCL statement, which ends with a semi-colon.
The second statement simply filters the input stream for rows with "EBAY" in the Symbol column and inserts those rows into FilteredTradesWindow.
Notice the italicized green text in the Queries tab. These are comments, each beginning with two hyphens (you can also begin a comment with two slashes or enclose comments between /* and */).
Notice the rows that appear in red. Both the latest row arriving in the stream and a row matching the filter in the Query statement appear in red.
In the window viewer, the latest row being added to the window is shown in red. The row being removed is shown in red with a line through it. Notice that the timestamp of the row being removed changes to reflect the time that it is removed.
This type of window is called a count-based window, because it maintains a specific number of rows. Once that number of rows is reached, a new row arriving on the stream displaces the oldest row in the window.
Notice that the window you created with CCL now appears in the Explorer view: