Contents
This topic describes AggregateByTime.sbapp, the Aggregate
operator time dimension sample.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click → .
-
Select operator from the Applications list.
-
Click OK.
StreamBase Studio creates a single project for the all operator samples.
By default, the sample files are installed in:
-
On Windows:
C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\operator -
On UNIX:
/opt/streambase/sample/operator
When you load the sample into StreamBase Studio, Studio copies the
sample project's files to your Studio workspace. StreamBase Systems
recommends that you use the workspace copy of the sample, especially on UNIX, where
you may not have write access to /opt/streambase. In
the default installation, the path to this sample in your Studio workspace is:
UNIX: ~/streambase-studio-n.m-workspace/sample_operator Windows XP: C:\Documents and Settings\username\My Documents\StreamBase Studion.mWorkspace\sample_operator Windows Vista: C:\Users\username\Documents\StreamBase Studion.mWorkspace\sample_operator
This sample demonstrates use of the time-based Aggregate operator. The time-based aggregate uses elapsed time to manage windows. This example uses 2-second windows to compute average price per share of symbols.
-
In the Package Explorer, double-click to open the
AggregateByTime.sbappapplication. Make sure the application is the currently active tab in the EventFlow Editor. -
Click the
Run button. This opens the
SB Test/Debug perspective and starts the application.
-
No output is displayed in the Application Output view yet, but the dequeuer is prepared to receive output. This view will eventually show the output of the application.
-
In the Feed Simulations view, right click on
AggregateByTime.sbfsand select . -
Observe application output in the Application Output view. The format is similar to the following:
Symbol=MSFT, TimeBasedAverage=26.05, start_time=2007-04-26 13:05:26.000-0400,
end_time=2007-04-26 13:05:28.000-0400
Symbol=AAPL, TimeBasedAverage=43.95, start_time=2007-04-26 13:05:26.000-0400,
end_time=2007-04-26 13:05:28.000-0400
Tip
If output rows are too long to see all the data, click a row to display its fields in the Display Fields pane below the table.
Average, start, and end times will all vary depending on the rate of input. For example, the first time you run the sample, the first tuple may occur just before the 2-second boundary and the second time it may occur just after the 2-second boundary. The averages will be different because the tuples are seen in different windows.
-
When done, press F9 or click the
Stop Running Application button.
This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.
-
Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.
-
In window 1, type:
sbd AggregateByTime.sbappThe window shows:
notice[StreamBaseServer] listening on port 10000
-
In window 2, type:
sbc dequeue AvgOutNo output is displayed at this point, but the dequeuer is prepared to receive output. This window will eventually show the output of the application.
-
In window 3, type:
sbfeedsim AggregateByTime.sbfsData starts flowing into the application.
Observe several lines in the dequeue window: The format is similar to the following:
MSFT,26.05,2007-04-26 13:29:52.000-0400,2007-04-26 13:29:54.000-0400
AAPL,43.95,2007-04-26 13:29:52.000-0400,2007-04-26 13:29:54.000-0400Average, start, and end times will all vary depending on where the tuples fall relative to the wall-clock time. For example, the first time you run the sample, the first tuple may occur just before the 2-second boundary and the second time may occur just after the 2-second boundary. The averages will be different because the tuples occur in different windows. The start times and end times will of course be different.
-
In window 3, type
sbadmin shutdownto terminate the daemon and dequeuer.
-
Launched StreamBase Studio.
-
Created (or subsequently used) the
sample_operatorproject. -
From the top menu, in the SB Authoring perspective, selected → → . Selected the
sample_operatorproject and enteredAggregateByTimeas the application name. -
Created an Input Stream:
-
Dragged an Input Stream from the palette to the EventFlow Editor.
-
Clicked the stream on the EventFlow Editor, which invoked the Input Stream Properties dialog window.
-
On the General tab, Name:
TradesIn -
On the Edit Schema tab, added:
-
Field Name:
Symbol, Type:string, Size:5 -
Field Name:
PricePerShare, Type:double
-
-
Clicked .
-
-
Created an Aggregate operator:
-
Dragged an Aggregate operator from the palette to the EventFlow Editor.
-
On the General tab, Name:
AggregateAvgByTime. -
Connect the TradesIn input stream to the AggregateAvgByTime operator.
-
-
Set up the AggregateAvgByTime operator:
-
On the Dimension tab, clicked the button. In the Edit Dimension dialog, added:
Name:
AggregateAvgByTimeDimType:
timeThe window will be established and evaluated based on elapsed time . When the specified time is elapsed, any desired calculations will take place, a tuple containing the desired results will be emitted, and the window will be closed. (See also the Aggregate Operator Field Dimension Sample.)
Opening policy: Open per: Advance:
2and Offset:0This indicates that a window should be opened for the time events. An Advance value of
2advances the window by two seconds. Since the advance value is the same as window size, windows will not overlap.Window size: Close and emit every
2The number of seconds after which the window will be closed.
Emission policy: Selected "No intermediate emissions based on this dimension."
"Emission policy" allows tuples to be emitted before the window closes. For example, one could emit a tuple every second during the 2-second window, rather than waiting for the window to close.
Optional windows Not applicable for time-based windows.
At this point, the Edit Dimensions dialog for AggregateAvgByTimeDim looks like this:
-
On the Aggregate Functions tab, unchecked the delta option, Output all input fields. Then added:
Output Field Name:
TimeBasedAverageExpression:
avg(PricePerShare)Calculates the average price per share of all the tuples in the window. For details about the available aggregate functions, see the StreamBase Expression Language and Functions topic in the Authoring Guide.
Output Field Name:
start_timeExpression:
openval('AggregateAvgByTimeDim')Shows the lower boundary of the window (not necessarily the actual time of the first tuple when the window opens).
Output Field Name:
end_timeExpression:
closeval('AggregateAvgByTimeDim')Shows the upper boundary of the window (not necessarily the actual time of the last tuple when the window closes).
For example:
-
On the Group Options tab, added:
Output Field Name:
SymbolExpression:
SymbolCreates a window for each set of tuples whose value for the
Symbolfield is the same. Note that the Output Field Name need not be the same as the input field in the expression. This is most useful when the expression is more complicated than just a field value.
-
-
Created an output stream:
-
Dragged an output stream from the palette to the EventFlow Editor.
-
On the General tab, Name:
AvgOut -
Connected the AggregateTrades operator to the AvgOut output stream.
-
