Custom C++ Aggregate Function Sample

The nth_value.sbapp sample application demonstrates how to use a custom C++ aggregate function.

This topic describes how to load and run the sample. For detailed information about the sample, see Creating Custom C++ Functions in the API Guide.

This Sample's Files

The sample has the following files:

  • A sample configuration file, sbd.sbconf, which tells the StreamBase Server to load the custom aggregate function.

  • A sample application, nth_value.sbapp, which uses the aggregate function.

  • Dynamically linked library for the custom aggregate function.

  • The source code for the custom aggregate function, NthValue.cpp

  • Either Microsoft Visual Studio .NET solution and project files (Windows) or a Makefile (UNIX), which can be used to build all of the sample source code. (In the Visual Studio project files, the paths to the StreamBase include files and libraries are configured for the standard default installation directory. You may need to adjust these paths for your PC.)

Running the Custom C++ Aggregate Function Sample

Notice that the custom function is declared in the nth_value.sbconf file:

<streambase-configuration>
    <global>
        <plugin file="./nth_value"/>
    </global>
    <server>
        <param name="tcp-port" value="10000"/>
    </server>
</streambase-configuration>

Running custom-aggregate-function in StreamBase Studio

  1. In the Package Explorer, double-click to open the nth-value.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Application Output view, leave the All selection.

  4. In the Manual Input view, select the input stream.

  5. Enter 1.1.

  6. Click Send Data, and observe no output. The aggregate window needs at least two tuples to produce output.

  7. Enter 2.2.

  8. Click Send Data, and observe 2.2 appears in the stream named output_window_size_2.

  9. Using the same send technique, send 5.6 and observe 5.6 on output_window_size_2 and 2.2 on output_window_size_3.

  10. Using the same send technique, send 3.5 and observe 3.5 on output_window_size_2 and 5.6 on output_window_size_3.

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

Running custom-aggregate-function in Terminal Windows

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.

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

  2. In window 1, launch a StreamBase Server on the sample application:

    sbd -f sbd.sbconf nth_value.sbapp

    Alternatively, if running in a 64-bit environment:

    sbd -f sbd-x64.sbconf nth_value.sbapp

  3. In window 2, run a dequeuer so that you can see the output that will be produced:

    sbc dequeue

  4. In window 3, run an enqueuer:

    sbc enqueue input

  5. Type numbers into the enqueuer, one number per line. After you enter the second number you should see output in window 2 from output_windows_size_2. After you enter the third number you should see output from output_window_size_3 as well as output_window_size_2. For example, if you enter the numbers 1.1, 2.2, 5.6, and 3.5, then you should see the following output from the dequeuer:

    output_window_size_2,2.2
    output_window_size_2,5.6
    output_window_size_3,2.2
    output_window_size_2,3.5
    output_window_size_3,5.6

  6. To see what happens when the aggregate function throws an exception, try enqueuing a number to the stream named input_that_will_cause_an_error.

  7. In window 3, press Ctrl+C to close the enqueuer. Then run sbadmin shutdown to close the server.

Back to Top ^

Importing This Sample into StreamBase Studio

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

  • From the top menu, click FileLoad StreamBase Sample.

  • Select this sample from the Extending StreamBase list.

  • Click OK.

StreamBase Studio creates a project for the sample.

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 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_custom-aggregate-function

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/custom-aggregate-function

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.