Aggregate Operator Two-Dimension Sample

About This Sample

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.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top-level menu, click File>Import Samples and Community Content.

  • Enter sample group to narrow the list of options.

  • Select Operator sample group from the Data Constructs and Operators category.

  • Click Import Now.

StreamBase Studio creates a single project for all the operator samples.

Running This Sample in StreamBase Studio

  1. In the Project Explorer view, open the sample you just loaded.

    If you see red marks on a project folder, wait a moment for the project to load its features.

    If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven>Update Project from the context menu.

  2. Open the src/main/eventflow/com.tibco.sb.sample.operator folder.

  3. Open the AggregateByDim.sbapp file and click the Run button. This opens the SB Test/Debug perspective and starts the module.

  4. In the Output Streams 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.

  5. In the Manual Input view, enter 1, AMAT, 20, and 1 in the seqnum, symbol, price, and time fields, respectively.

  6. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  7. Enter 2, AMAT, 21, and 11 in the seqnum, symbol, price, and time fields, respectively.

  8. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  9. Enter 3, AMAT, 22, and 21 in the seqnum, symbol, price, and time fields, respectively.

  10. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  11. Enter 4, AMAT, 23, and 31 in the seqnum, symbol, price, and time fields, respectively.

  12. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  13. Enter 5, AMAT, 24, and 41 in the seqnum, symbol, price, and time fields, respectively.

  14. Click Send Data, and observe this line in the Output Streams view:

    symbol=AMAT, numberShares=5, averagePricePerShare=22.0,
    lowerBoundTimeWindow=0.0, upperBoundTimeWindow=60.0, firstSeqnum=1, lastSeqnum=5

    This 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 Output Streams grid, click a row to display its field data in the Display Fields pane below the table.

  15. Enter 6, AMAT, 25, and 61 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.

  16. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  17. Enter 7, AMAT, 26, and 119 in the seqnum, symbol, price, and time fields, respectively.

  18. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  19. Enter 8, AMAT, 27, and 121 in the seqnum, symbol, price, and time fields, respectively.

  20. Click Send Data, and observe this line in the Output Streams view:

    symbol=AMAT, numberShares=3, averagePricePerShare=25.5,
    lowerBoundTimeWindow=60.0, upperBoundTimeWindow=120.0, firstSeqnum=6, lastSeqnum=7

  21. Enter 9, AMAT, 26, and 150 in the seqnum, symbol, price, and time fields, respectively.

  22. Click Send Data, and observe that no output is displayed yet in the Output Streams view.

  23. Enter 10, AMAT, 26, and 151 in the seqnum, symbol, price, and time fields, respectively.

  24. Click Send Data, and observe this line in the Output Streams view:

    symbol=AMAT, numberShares=3, averagePricePerShare=28,
    lowerBoundTimeWindow=120.0, upperBoundTimeWindow=180.0, firstSeqnum=8, lastSeqnum=10

  25. When done, press F9 or click the Terminate EventFlow Fragment button.

Sample Location

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 permission problems. The default workspace location for this sample is:

studio-workspace/sample_operator

See Default Installation Directories for the default location of studio-workspace on your system.