Contents
This topic explains AggregateByTup.sbapp, the Aggregate
operator tuple dimension sample.
There are two examples of the tuple-based aggregation in this sample. The first computes a moving average of shares based on a fixed number of trades. The second demonstrates how to create a sequence number using a window that does not close and which emits a value for each tuple received.
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 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 two uses of the tuple-based Aggregate operator. The first uses a tuple-based Aggregate operator to calculate a moving average of price for each of four successive trades of a given stock. It also ensures that the aggregation works for intermixed tuples for different stocks. The second provides a sequence number for input tuples, using a window which does not close and the emit feature to emit an output tuple for every input tuple.
-
In the Package Explorer, double-click to open the
AggregateByTup.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.
-
In the Application Output view, select the AvgPricePSOut output stream. No output is displayed at this point, but the dequeuer is prepared to receive output. This view will eventually show the output of the application.
-
In the Manual Input view, select the TradesIn input stream.
-
Enter
AMATand23in the Symbol, and PricePerShare fields, respectively. -
Click , and observe that no output is displayed yet in the Application Output view.
-
Enter
AMATand24in the Symbol, and PricePerShare fields, respectively. -
Click , and observe that no output is displayed yet in the Application Output view.
-
Again enter
AMATand24in the Symbol, and PricePerShare fields, respectively. -
Click . There is still no output in the Application Output view.
-
Enter
AMATand25in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=24.0
Tip
If output data is too long to easily see in the Application Output table, click a row to display its field data in the Display Fields pane below the table.
-
Enter
AMATand20in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=23.25
-
Enter
AMATand21in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=22.5
-
Enter
AMATand21in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=21.75
-
Enter
AMATand22in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=21.0
-
Enter
INTCand27in the Symbol, and PricePerShare fields, respectively. -
Click , and observe no further output is displayed yet in the Application Output view.
-
Enter
INTCand28in the Symbol, and PricePerShare fields, respectively. -
Click , and observe no further output is displayed yet in the Application Output view:
-
Enter
AMATand22in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=21.5
-
Enter
INTCand27in the Symbol, and PricePerShare fields, respectively. -
Click , and observe no further output is displayed yet in the Application Output view:
-
Enter
AMATand23in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=AMAT, MovingAverage=22.0
-
Enter
INTCand28in the Symbol, and PricePerShare fields, respectively. -
Click , and observe this line in the Application Output view:
Symbol=INTC, MovingAverage=27.5
-
Next, view the sequence number.
-
In the Application Output view, select the SeqOut output stream. No output is displayed at this point, but the dequeuer is prepared to receive output.
-
Change from Manual Input to the Feed Simulations view.
-
Right click on
SeqFeed.sbfsand select . -
Notice on output how sequence_number increases by
1. -
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 AggregateByTup.sbappThe window shows
notice[StreamBaseServer] listening on port 10000. -
In window 2, type:
sbc dequeue AvgPricePSOutNo 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:
sbc enqueue TradesInThe sbc command is now awaiting keyboard input. Then type:
AMAT,23No output is displayed yet in the dequeue window.
-
Type:
AMAT,24No output is displayed yet in the dequeue window.
-
Type:
AMAT,24No output is displayed yet in the dequeue window.
-
Type:
AMAT,25Observe this line in the dequeue window:
AMAT,24 -
Type:
AMAT,20Observe this line in the dequeue window:
AMAT,23.25 -
Type:
AMAT,21Observe this line in the dequeue window:
AMAT,22.5 -
Type:
AMAT,21Observe this line in the dequeue window:
AMAT,21.750000 -
Type:
AMAT,22Observe this line in the dequeue window:
AMAT,21 -
Type:
INTC,27No further output is displayed yet in the dequeue window.
-
Type:
INTC,28No further output is displayed yet in the dequeue window.
-
Type:
AMAT,22Observe this line in the dequeue window:
AMAT,21.5 -
Type:
INTC,27No further output is displayed yet in the dequeue window.
-
Type:
AMAT,23Observe this line in the dequeue window:
AMAT,22 -
Type:
INTC,28Observe this line in the dequeue window:
INTC,27.5 -
In window 3, press Ctrl-Z (Windows) or Ctrl-D (UNIX).
The sbc enqueue command will exit.
-
In window 2, press Ctrl-C.
The sbc dequeue command will exit.
-
In window 2, type:
sbc dequeue SeqOut -
In window 3, type:
sbfeedsim -a TradesIn2Observe the sequence numbers in the dequeue window
-
Stop feedsim:
In window 3, press Ctrl-C.
-
In window 3, type the following command to terminate the server and dequeuer:
sbadmin shutdown
-
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 enteredAggregateByTupas the application name. -
Created an input stream for the moving average:
-
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:
AggregateTrades. -
Connect the TradesIn input stream to the AggregateTrades operator.
-
-
Set up the AggregateTrades operator:
-
On the Dimension tab, clicked the button. In the Edit Dimension dialog, added:
Name: AggregateTradesDim
Type:
tuple.The buffer set up for each window will contain the specified number of tuples. When the buffer contains all the tuples required for the window, 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:
1.This indicates that a window should be open for a group of tuples. An Advance value of
1advances the window by one tuple. A new window will be created as each tuple enters the system (containing that tuple), ready to accept the next(wherenn=window size - 1) tuples. This means that windows overlap, and in this case, a given tuple may participate in up to four windows.Window size: Close and emit every
4The number of tuples in the buffer for this window.
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 30-second window, rather than waiting for the window to close.
Optional windows: Unchecked the Create partial windows checkbox.
When set, this option creates partial windows which encompass the values that would have occurred before the arrival of the first tuple. For example, where Advance is less than Size, additional windows would be opened to include the first tuple; these windows would start before the Time in the first tuple.
At this point our Edit Dimension dialog looks like this:
Clicked .
-
On the Aggregate Functions tab, unchecked the delta option, Output all input fields. Then added:
Output Field Name:
MovingAverageExpression:
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.
-
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:
AvgPricePSOut -
Connected the AggregateTrades operator to the AvgPricePSOut output stream.
That completed our work for the TradesIn stream.
-
To create the sequence portion of the AggregateByTup sample:
-
Created a separate Input Stream for the sequence:
-
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:
TradesIn2 -
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:
AggregateSeq. -
Connected the TradesIn2 Input Stream to the AggregateSeq operator.
-
-
Set up the AggregateSeq operator:
-
On the Dimension tab, clicked the button. In the Edit Dimension dialog, added:
Name:
AggregateSeqDimType:
tuple.The buffer set up for each window will contain the specified number of tuples. When the buffer contains all the tuples required for the window, any desired calculations will take place, a tuple containing the desired results will be emitted, and the window will be closed.
Opening policy: Do not open window based on this dimension
Window size: Do not close window based on this dimension
That combination of settings creates a window that never closes.
Emission policy: Intermediate intermission every
1"Emission policy" allows tuples to be emitted before the window closes. In this case, as every tuple arrives, one tuple is emitted with the sequence number.
Optional windows: Unchecked the Open windows before first tuple checkbox.
At this point our Edit Dimensions dialog for AggregateSeqDim looks like this:
-
On the Aggregate Functions tab, unchecked the delta option, Output all input fields. Then added:
Output Field Name:
sequence_numExpression:
count()As each tuple arrives in the aggregate operator, it will be counted. Since each tuple will cause a tuple to be emitted, the emitted tuple will contain a value one greater than the previous tuple. For details about the available aggregate functions, see the StreamBase Expression Language and Functions topic in the Authoring Guide.
-
On the Group Options tab, did not add any groups.
This example creates a sequence number that is global across all Symbols. If there were a group by on the Symbols field, each Symbol would have its own unique sequence.
-
-
Created an output stream:
-
Dragged an output stream from the palette to the EventFlow Editor.
-
On the General tab, Name:
SeqOut -
Connected the AggregateSeq operator to the SeqOut output stream.
-
