StreamBase includes many built-in functions, but if you need more you can use the
StreamBase API to add your own. The StreamBase Java Function wizard can
generate stubs for custom functions, which you can then customize for your specific
needs by replacing the // TODO sections in the generated
Java source with your implementation code. Optionally,
you can also use the wizard to create an alias for your function. You can then call the
functions in expressions within a StreamBase application, using either the
calljava function or the alias.
Use this wizard to start defining one or more StreamBase custom Java functions.
To open the wizard, click → → .
To define a function, edit the fields as follows:
-
Type: Choose either simple or aggregate:
-
Simple functions can be used in any StreamBase operator.
-
Aggregate functions are available in the Aggregate operator and Query operator. They are evaluated over groups of arguments (such as the values in a window of an Aggregate operator) and return a single result.
-
-
Source folder: The path to an existing folder in which to create your function class. Specify the
java-srcfolder that StreamBase projects have by default, or any other java source folder. -
Choose where to create your function. For an aggregate function, you must create a new class. For a simple function, you can either add your function to an existing class, or create a new one from scratch:
-
Existing Class: If you choose this option, click to select the class.
-
New Class: For an aggregate function, this option is preselected and cannot be changed. With this option selected, you must also specify the remaining two fields:
-
Package: While you could use the default package, it is recommended that you specify one, such as com.mycompany.mysbapps.functions. Click to choose an existing one.
-
Name: Enter A name for your new class, such as
MyFunctions.
-
-
For example:
When you are ready, click to proceed to the next page. Depending on your selection, go on to either the Defining Simple Function Properties or Defining Aggregate Function Properties section for details.
The Add/Edit page is the second page of the StreamBase Java wizard for a simple custom function. Use it to define one or more functions.
-
Click . Then, in the StreamBase Simple Function dialog:
-
Specify the Function Name and Return Type.
-
If the function will have arguments, add them now: For each argument choose a data type using the Select Type control, then click to insert a new entry of the specified type in the Argument List.
Argument names are automatically assigned in sequential fashion (arg0, arg1, ...). You can edit the names in the Argument List. You can also change the data types if you need to.
The order of arguments expected in the function is determined by their order in the Argument List. You can use the Up and Down buttons to change the order.
-
Click to save the function.
-
-
Click again in the Simple Function page to define any additional functions.
-
Optionally, use the other control buttons to edit, remove, or reorder your functions.
-
If you checked an option to add the function to an existing class, click the Insertion Point control and indicate where you want the new function to be inserted. Choose one of these options:
-
First method
-
Last method
-
After
method, wheremethodis the name of an existing method. The menu contains an option for each method in the class. -
Current cursor position (if target file was already open)
-
-
If you want to use aliases in your function, check the Create Function Aliases box, and click . In the Create StreamBase Custom Function Aliases dialog:
-
Choose the projects to create your aliases in[1].
-
The Aliases grid shows the currently defined functions and the alias names that will be created. Optionally, you can change the default alias names in the grid.
-
If you defined any functions with String return types, specify the length to use.
-
Click .
-
-
When you are done, click .
In the StreamBase Custom Java Aggregate Function page, the wizard will
help you specify arguments for one or more required accumulate() methods; then you will select the return type for
calculate() to finish creating the aggregate
function.
A brief explanation: All custom Java Aggregate classes extend AggregateWindow. Your subclass must implement an accumulate method that corresponds to the argument number and
data types that the calljava expression in the
Aggregate operator refers to.
Your subclass must also implement a calculate method.
When data is sent from the aggregate window, the calculate method must return a type mapping to a
StreamBase data type. The AggregateWindow object is likely to be used several times. At the
start of each new use, the init() method will be
called. Then accumulate and calculate may be called. Finally, at the end of each use,
release will be called.
You can find these elements in the java file generated
by the wizard.
In the StreamBase Custom Java Aggregate Function page:
-
Add one or more accumulate methods. To add a method, click . Then in the New Arguments dialog, define one or more method arguments:
-
For each argument, specify a data type using the Select Type control, and click to insert a new entry of the specified type in the Argument List.
-
Optionally, change the generated argument names in the Argument List.
-
The order of arguments expected in the function is determined by their order in the Argument List. You can also use the Up and Down buttons to change the order.
-
When you have defined all the arguments, click to close the New Arguments dialog.
-
-
Specify the return data type for calculate().
-
You can click again in the Aggregate Function page to add any number of accumulate() methods to the function.
-
Optionally, use the other control buttons to edit or remove methods. You can also reorder them on the wizard page.
-
If you want to use aliases in your function, check the Create Function Aliases box, and click . In the Create StreamBase Custom Function Aliases dialog:
-
Choose the projects to create your aliases in[1].
-
The Aliases grid shows the currently defined functions and the alias names that will be created. Optionally, you can change the default alias names in the grid.
-
If you defined any functions with String return types, specify the length to use.
-
Click .
-
-
When you are done, click .
When you click , the wizard generates a single
java source file containing all the functions you
defined, and opens the file in the Java Editor.
Here is a portion of a simple generated java file. As
the sample shows, all custom simple functions must be in a public Java class as a
public static method. In your file, look for the // TODO
comments and complete the coding for your function:
package com.mycompany.mysbapps.functions;
public class MyFunctions {
.
.
.
public static int times2(int volume) {
// TODO Implement function here
return 0;
}
-
For the Java API Javadoc, see the StreamBase Application Programming Interfaces topic of the StreamBase Reference Guide.
-
For information about function aliases, see the custom-functions section of the sbd configuration file documentation, StreamBase Server Configuration XML.
[1] The list of projects is limited to those whose build paths include your target project (the project where your function's class file will be created). To make other projects available, see Adding Other Projects to the Java Build Path.
