Contents
The nth.sbapp
sample application demonstrates a custom Java aggregate function, calljava
, that performs an aggregation on three tuples. The calljava
function for aggregate expressions was built by extending the AggregateWindow
class, which is documented in the StreamBase Java Client library.
In the application, each input tuple contains a single integer field. The Aggregate operator has a window size of 3, so it It waits until three tuples are input before emitting a tuple. The output tuple contains four fields, each generated by an expression in the Aggregate operator:
-
firstval(val)
-
calljava('com.streambase.sb.sample.Nth', 2, val)
-
calljava('com.streambase.sb.sample.Nth', 3, val)
-
calljava('com.streambase.sb.sample.Nth', 4, val)
In the first expression, the built-in function firstval
is used to return the value in the first tuple.
In the remaining expressions calljava
is used to do the same thing: it returns the value of a specified tuple.
Notice that the last expression references a fourth tuple. However, only three tuples should be in the window when it closes, because the aggregate window size is 3. This reference to a non-existent tuple is a deliberate mistake — read on and run the sample to find out the result.
This topic describes how to load and run the nth.sbapp
sample. For more information about custom Java functions, see the topic, Using the StreamBase Java Function Wizard, in the API Guide.
The sample has the following files:
-
The source code for the function,
Nth.java
-
A sample configuration file,
sbd.sbconf
, which tells StreamBase Server to load the custom simple function. -
A sample application,
nth.sbapp
, which uses the function.
-
In the Package Explorer, double-click to open the
nth.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.
-
When the server starts, StreamBase Studio switches to the SB Test/Debug perspective.
-
On the Manual Input view, enter the following values in the val field, pressing
after each entry:1
2
3 -
Observe the tuples in the Application Output view. No tuples are output until three input tuples are sent, because the aggregate's window size is set to 3. The output you should observe is:
first=1.0, second=2.0, third=3.0, fourth=null
These values are returned by the
calljava
functions for each field. Notice that the fourth field is always null because there are only three tuples in the window. -
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, launch the StreamBase Server for the sample application:
sbd -f sbd.sbconf nth.sbapp -p 10000
-
In window 2, run a dequeuer so that you can see the output that will be produced:
sbc dequeue
-
In window 3:
-
Run an enqueuer:
sbc enqueue InputStream1
-
Type these numbers into the enqueuer, one number per line:
1
2
3
-
-
In window 2, you should see this output from the application:
OutputStream1,1,2,3,null
These values are returned by the
calljava
functions for each field. Notice that the fourth field is always null because there are only three tuples in the window. -
In window 2, type: Ctrl+Z (Windows) or Ctrl+D (UNIX). The sbc command exits.
-
In window 3, type: Ctrl+Z (Windows) or Ctrl+D (UNIX). The sbc command exits.
-
In window 3, type:
sbadmin shutdown
. This terminates the server and the dequeuer.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click
→ . -
Select this sample from the Extending StreamBase list.
-
Click OK.
StreamBase Studio creates a project for the sample.
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-java-aggregate
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-java-aggregate
See Default Installation Directories for the default location of studio-workspace
on your system.