Aggregate Operator Time Dimension Sample

About This Sample

This topic describes AggregateByTime.sbapp, the Aggregate operator time dimension sample.

Running AggregateByTime.sbapp in StreamBase Studio

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, grouping its computations by stock symbol.

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

  2. Open the src/main/eventflow folder.

  3. Open the package folder (most samples contain a single package folder. Open the top-level package folder if your sample contains more than one folder).

  4. Open the AggregateByTime.sbapp application and click the Run button. This opens the SB Test/Debug perspective and starts the application.

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

    If red marks do not resolve themselves in a moment, select the project and right-click MavenUpdate Project from the context menu.

  5. 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.

  6. In the Feed Simulations view, right click on AggregateByTime.sbfs and select Run.

  7. Observe application output in the Application Output view. The format is similar to the following:

    12:06:42 AvgOut stock=MSFT, TimeBasedAverage=26.05, 
        start_time=2013-12-05 12:06:40.000-0500, end_time=2013-12-05 12:06:42.000-0500, 
        first_time=2013-12-05 12:06:40.513-0500, last_time=2013-12-05 12:06:41.620-0500, n=4
    12:06:44 AvgOut stock=AAPL, TimeBasedAverage=43.95, 
        start_time=2013-12-05 12:06:42.000-0500, end_time=2013-12-05 12:06:44.000-0500, 
        first_time=2013-12-05 12:06:42.223-0500, last_time=2013-12-05 12:06:43.725-0500, n=4

    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.

    Each row is an average for a group. The output fields represent the following, per window:

    • Symbol — The stock symbol, used to sort tuples into groups

    • Average — Time-based Average price per share, calculated with the aggregate function avg()

    • start_time — The time the window opened (its openval())

    • end_time — The time the window closed (its closeval())

    • first_time — Time at which the first tuple arrived in the window (firstval(time))

    • last_time — Time at which the last tuple arrived in the window (lastval(time))

    • n — The number of tuples over which average price per share was computed (count())

    Averages and 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.

  8. When the feed simulation has stopped, you can observe how emission occurs in two seconds (the difference between end_time and start_time is 2.0) regardless of whether the operator continues to receive input. If you run again, speeding up or slowing down the feed simulation, you will see n go down or up, accordingly.

  9. Click the Manual Input tab, type a string for Symbol and number for PricePerShare, and then hit Send Data. After two seconds, you will observe the operator time out and emit output.

  10. When done, press F9 or click the Stop Running Application button.

Importing This Sample into StreamBase Studio

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

  • From the top menu, click FileLoad StreamBase Sample.

  • Type operator to narrow the list of options.

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

  • Click OK.

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

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.