The app.sbapp
sample application demonstrates three custom Java simple functions using three independent flows.
The top flow (Input stream TriangleSidesIn) computes the hypotenuse of a right triangle from two doubles representing the lengths of the right angle sides using several different methods.
The application generates the output using these three expressions in a Map operator:
-
hypotenuse(x, y)
-
hypot(x, y)
-
calljava('java.lang.Math', 'hypot', x, y)
The first expression uses a function alias defined in the sbd.sbconf
file that will use the Hypotenuse class defined in the Hypotenuse.java
sample file. The second expression uses a function alias that will use the java.lang.math
method. The final expression uses calljava
function to call the java.lang.math
method directly.
The two lower flows illustrate how custom Java functions can take variable-length arguments to process variable-length arguments
("varargs"). The functions have aliases in the project's sbd.sbconf file that enables them to be called directly, like the
custom function calculate used in the first flow, which is aliased to the name hypoteneuse
. Note that a function alias can have the same as the function it refers to or can have a different name.
The middle flow (input stream VarArgsIsIn) uses the custom function IsIn
, defined in class com.streambase.sample.VarArgs
, to determine if input string needle exists in any of the following argument (strings haystack1 ... haystackn
) and returns a boolean result.
The bottom flow (input stream VarArgsSum) uses the custom function SumAll
in that same class to compute the sum of any number of doubles and returns a double result.
To keep the example simple, the custom functions do not validate their inputs at runtime.
This topic describes how to load and run the app.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 Java source code files
VarArgs.java
andHypotenuse.java
-
A sample configuration file,
sbd.sbconf
, which tells StreamBase Server to load the custom function and defines function aliases. -
A sample application,
app.sbapp
, which shows different ways to invoke these functions.
-
In the Package Explorer, double-click to open the
app.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.
-
Enter data for the TriangleSidesIn stream
-
On the Manual Input view, select the TriangleSidesIn input stream.
-
Enter the following values in the
x
andy
fields, then press :x: 2
y: 2 -
In the Application Output view, observe the tuple emitted when you press
. You should see the three calculated hypotenuse values are all identical and are approximately 2.83.
-
-
Enter data for the VarArgsIsIn stream
-
On the Manual Input view, select the VarArgsIsIn input stream. Notice the four input fields: needle (the string to search for), and haystack1 and haystack2, strings to search within. If either of the haystacks contains needle, the output field IsNeedleInHaystack will be true, else false.
-
Enter the following unquoted strings in the respective fields:
needle: banana
haystack1: time flies like an arrow
haystack2: null -
In the Application Output view, observe the tuple emitted when you press
. The value for IsNeedleInHaystack is false because neither haystack1 nor haystack2 contains the string "banana". -
Now in the haystack2 field, enter:
fruit flies like a banana
-
Press
again. -
In the Application Output view, observe the tuple emitted when you press
. The value for IsNeedleInHaystack is true because haystack2 contains the string "banana".You can add more string fields to the schema and pass them as varargs to the isIn Java function by updating the IsIn Map operator's output expression for IsNeedleInHaystack to include them. That expression initially is
isIn(needle, haystack1, haystack2)
. Simply append new string fields to the argument list to observe that they are processed by theisIn
Java function..
-
-
Enter data for the VarArgsSums stream
-
On the Manual Input view, select the VarArgsSum input stream. It has three double fields; value1, value2, and value3.
-
Enter the following numbers in the respective fields:
value1: 1.
value2: 2.
value3: null -
In the Application Output view, notice that the value of the field sum in the tuple that was just emitted is 3.
-
Stop the application by pressing F9 or click the Stop Running Application button.
-
With app.sbapp selected in the Eventflow editor, select the SumAll Map operator. In the Output Settings tab, notice the added expression
sumAll(value1,value2)
, which takes two arguments. -
Change the expression to
sumAll(value1,value2,value3)
, save the application, and run it again. -
In the Manual Input view, enter the following numbers in the respective fields:
value1: 1.
value2: 2.
value3: 3. -
Press
again. -
In the SumAllOut stream in the Application Output view, notice that the value of the field sum in the tuple that was just emitted is now 6.
Because the Java function
sumAll()
takes a variable list of arguments, you can add any number of double fields to the input schema and reference them in thesum
output expression.
-
-
When done, press F9 or click the Stop Running Application button.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click
→ . -
Select the custom-java-function sample from the Extending StreamBase list.
-
Click OK.
StreamBase Studio creates a project for each 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-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-java-function
See Default Installation Directories for the default location of studio-workspace
on your system.