Contents
This sample demonstrates one use of the Aggregate operator. The time-based aggregate uses elapsed time to manage windows. This example uses two-second windows to compute the average price per share of symbols.
Consider the following problem: You are interested in the average price per share of a stock over some number of trades. You also want to know if the stock is active or not. If you get fewer than the requisite number of trades during some time period, then you conclude that the stock is relatively inactive. If you see more than that number of trades in the time period, the stock is very active.
This problem can be solved using an Aggregate with two window dimensions, one for the number of trades (as tuples), and another for time period. The time period is computed as a field-based aggregate using a timestamp field. In the following example, the first tuple emitted from Aggregate2Dimensions shows the average of five tuples. The second emitted tuple shows the average of only two tuples because only those two tuples fall within the time window as defined by the second dimension. The third tuple is emitted because five tuples had been received by Aggregate2Dimensions since the last five tuple group. However, because the first two of those input tuples had been calculated into the second emitted tuple, there are only three tuples available to be used to calculate the average. The numberShares, firstSeqnum, and lastSeqnum fields reflect this fact.
-
In the Package Explorer, double-click to open the
AggregateByDim.sbapp
application. 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: the first tuple received will open a window that will close after receiving either five tuples or a tuple with time greater than or equal to
60
. -
In the Manual Input view, enter
1
,AMAT
,20
, and1
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
2
,AMAT
,21
, and11
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
3
,AMAT
,22
, and21
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
4
,AMAT
,23
, and31
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
5
,AMAT
,24
, and41
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe this line in the Application Output view:symbol=AMAT, numberShares=5, averagePricePerShare=22.0,
lowerBoundTimeWindow=0.0, upperBoundTimeWindow=60.0, firstSeqnum=1, lastSeqnum=5This input causes the Aggregate operator to close the first window, which triggers the release of the output tuple.
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
6
,AMAT
,25
, and61
in the seqnum, symbol, price, and time fields, respectively.This input causes a new window to open. Like the first window, it will close after receiving either five tuples or a tuple with time greater than or equal to 60.
-
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
7
,AMAT
,26
, and119
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
8
,AMAT
,27
, and121
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe this line in the Application Output view:symbol=AMAT, numberShares=3, averagePricePerShare=25.5,
lowerBoundTimeWindow=60.0, upperBoundTimeWindow=120.0, firstSeqnum=6, lastSeqnum=7 -
Enter
9
,AMAT
,26
, and150
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe that no output is displayed yet in the Application Output view. -
Enter
10
,AMAT
,26
, and151
in the seqnum, symbol, price, and time fields, respectively. -
Click
, and observe this line in the Application Output view:symbol=AMAT, numberShares=3, averagePricePerShare=28,
lowerBoundTimeWindow=120.0, upperBoundTimeWindow=180.0, firstSeqnum=8, lastSeqnum=10 -
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 AggregateByDim.sbapp
The window shows
notice[StreamBaseServer] listening on port 10000
. -
In window 2, type:
sbc dequeue AvgPricePSOut
No 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 TradesIn
The sbc command is now awaiting keyboard input. Then type:
1,AMAT,20,1
No output is displayed yet in the dequeue window.
-
Type:
2,AMAT,21,11
No output is displayed yet in the dequeue window.
-
Type:
3,AMAT,22,21
No output is displayed yet in the dequeue window.
-
Type:
4,AMAT,23,31
No output is displayed yet in the dequeue window.
-
Type:
5,AMAT,24,41
Observe this line in the dequeue window:
AMAT,5,22,0,60,1,5
-
Type:
6,AMAT,25,61
No output is displayed yet in the dequeue window.
-
Type:
7,AMAT,26,119
No output is displayed yet in the dequeue window.
-
Type:
8,AMAT,27,121
Observe this line in the dequeue window:
AMAT,2,25.5,60,120,6,7
-
Type:
9,AMAT,28,150
No output is displayed yet in the dequeue window.
-
Type:
10,AMAT,29,151
Observe this line in the dequeue window:
AMAT,3,28,120,180,8,10
-
Press Control+Z (Windows) or Control+D (UNIX).
The sbc process will exit.
-
In window 3, type:
sbadmin shutdown
The sbadmin shutdown command terminates the server and dequeuer.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click
→ . -
Select operator from the Data Constructs and Operators category.
-
Click OK.
StreamBase Studio creates a single project for all the operator samples.
When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.
Important
Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.
Using the workspace copy of the sample avoids the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:
studio-workspace
/sample_operator
See Default Installation
Directories for the location of studio-workspace
on your system.
In the default TIBCO StreamBase installation, this sample's files are initially installed in:
streambase-install-dir
/sample/operator
See Default Installation
Directories for the location of streambase-install-dir
on your system. This location
may require administrator privileges for write access, depending on your platform.