Custom Java Simple Function Sample

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.

This Sample's Files

The sample has the following files:

  • The Java source code files VarArgs.java and Hypotenuse.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.

Running the Custom Java Simple Function Application

Running app.sbapp in StreamBase Studio

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

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

  3. When the server starts, StreamBase Studio switches to the SB Test/Debug perspective.

  4. Enter data for the TriangleSidesIn stream

    1. On the Manual Input view, select the TriangleSidesIn input stream.

    2. Enter the following values in the x and y fields, then press Send Data:

      x: 2
      y: 2

    3. In the Application Output view, observe the tuple emitted when you press Send Data. You should see the three calculated hypotenuse values are all identical and are approximately 2.83.

  5. Enter data for the VarArgsIsIn stream

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

    2. Enter the following unquoted strings in the respective fields:

      needle:    banana
      haystack1: time flies like an arrow
      haystack2: null

    3. In the Application Output view, observe the tuple emitted when you press Send Data. The value for IsNeedleInHaystack is false because neither haystack1 nor haystack2 contains the string "banana".

    4. Now in the haystack2 field, enter:

      fruit flies like a banana

    5. Press Send Data again.

    6. In the Application Output view, observe the tuple emitted when you press Send Data. 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 the isIn Java function..

  6. Enter data for the VarArgsSums stream

    1. On the Manual Input view, select the VarArgsSum input stream. It has three double fields; value1, value2, and value3.

    2. Enter the following numbers in the respective fields:

      value1: 1.
      value2: 2.
      value3: null

    3. In the Application Output view, notice that the value of the field sum in the tuple that was just emitted is 3.

    4. Stop the application by pressing F9 or click the Stop Running Application button.

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

    6. Change the expression to sumAll(value1,value2,value3), save the application, and run it again.

    7. In the Manual Input view, enter the following numbers in the respective fields:

      value1: 1.
      value2: 2.
      value3: 3.

    8. Press Send Data again.

    9. 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 the sum output expression.

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

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 the custom-java-function sample from the Extending StreamBase list.

  • Click OK.

StreamBase Studio creates a project for each 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 permission problems. The default workspace location for this sample is:

studio-workspace/sample_custom-java-function

See Default Installation Directories for the default 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.