Java primitive operator guide
This guide demonstrates creating a Java primitive operator, including implementing the operator and testing the operator.
This guide uses an example that creates a Java primitive operator to produce the sum of two numbers.
Implementing the Java primitive operator
This section guides you through the steps of creating the primitive operator and configuring the Java class implementation for your Java primitive operator.
Creating the Java primitive operator
This task demonstrates how to create the Java primitive operator.
- Set up your Java development environment.
- Add your Streams 5.5 instance to the Streams Explorer in VS Code if you haven’t already.
- Bring up the Command Palette and select Create Java Primitive Operator.
- Provide the following information:
- Project folder path: Use the Browse… button to create and select an empty folder on your machine called
MyJavaProject
. The primitive operator files will be created in this folder. Example:/Users/someuser/Documents/streams/MyJavaProject
. - Operator namespace: Specify
sample.sum
as the namespace. Your Java primitive operator will be created in this namespace. - Operator name: Specify
MyJavaOp
as the name of your Java primitive operator. - Operator processing pattern: Use the default selected pattern. The pattern identifies whether the operator has input ports, output ports, or both, and the type of tuple flow the operator provides. Each pattern uses templates for generating the operator model and Java source files.
- Project folder path: Use the Browse… button to create and select an empty folder on your machine called
- Click the Create button to create the skeleton Java primitive operator.
- If you have multiple Streams instances, you will be prompted to select an instance.
- Add the Log4j 1.2 dependency by following the last step here.
Customizing the Java class implementation
This task demonstrates how to customize the implementation of the Java class (.java
file) for your Java primitive operator.
- Open the Java source file
MyJavaOp.java
. This is located in theMyJavaProject/impl/java/src/sample.sum
folder. -
Locate the
process
method and replace the body with the following code (in between the curly braces):// Create a new tuple for output port 0. StreamingOutput<OutputTuple> outStream = getOutput(0); OutputTuple outTuple = outStream.newTuple(); // Copy across all matching attributes. outTuple.assign(tuple); // Set the sum to the "c" attribute in the tuple. outTuple.setInt("c", tuple.getInt("a") + tuple.getInt("b")); // Submit new tuple to output port 0. outStream.submit(outTuple);
- Select File > Save to save the changes to
MyJavaOp.java
.
Testing the Java primitive operator
This section guides you through the steps of building your Java primitive operator, creating a SPL application that invokes your operator, and then building and running the application to test your operator.
Building the Java primitive operator
This task demonstrates how to build the Java primitive operator. This creates a toolkit that you can use in your SPL application.
- Right-click on the
MyJavaOp.java
file and select Build Java Primitive Operator. - If you have multiple Streams instances, you will be prompted to select an instance.
- Check the notifications and build output to confirm that the primitive operator was built successfully. If it was successful, select the Add Toolkit to Toolkit Path button in the notification that appears. If you do not have a toolkit path specified, you will be prompted to specify a path. This makes the toolkit available for use in streaming applications.
Creating the SPL application
This task demonstrates how to create a SPL application that invokes the Java primitive operator.
- Bring up the Command Palette and select Create SPL Application.
- Provide the following information:
- Application folder path: Select an empty folder on your machine called
TestJavaProject
. Example:/Users/someuser/Documents/streams/TestJavaProject
. - Namespace: Specify
sample.sum.test
as the namespace. Your main composite will be created in this namespace. - Main composite name: Specify
Main
as the name of your main composite.
- Application folder path: Select an empty folder on your machine called
- Add the toolkit as a dependency.
- Open the toolkit information model file
info.xml
. This is located in theTestJavaProject
folder. - Locate the
<info:dependencies/>
element and replace it with the following code:<info:dependencies> <info:toolkit> <common:name>MyJavaProject</common:name> <common:version>1.0.0</common:version> </info:toolkit> </info:dependencies>
- Open the toolkit information model file
-
Edit the SPL source.
- Open SPL source file
Main.spl
. This is located in theTestJavaProject/sample.sum.test
folder. - Import the
MyJavaOp
operator by entering the followinguse
directive at the beginning of the file afternamespace sample.sum.test;
:use sample.sum::MyJavaOp;
-
Enter the following code for the
Main
composite (replace the starter code in between the curly braces):graph stream<int32 a, int32 b> InStream = Beacon() { logic state: mutable int32 n = 0, m = 0; param iterations: 10u; output InStream: a = ++n, b = 10 - ++m; } // Invoke the Java primitive operator MyJavaOp stream<int32 a, int32 b, int32 c> Sum = MyJavaOp(InStream) {} () as PrintSum = Custom(Sum) { logic onTuple Sum: println((rstring) a + " + " + (rstring) b + " = " + (rstring) c); }
- Select File > Save to save the changes to
Main.spl
.
- Open SPL source file
Building and running the SPL application
This task demonstrates how to build and run the SPL application to test your Java primitive operator.
- Build and run the SPL application.
- Right-click on the
Main.spl
file and select Build and Submit Job. - If you have multiple Streams instances, you will be prompted to select an instance.
- When the build completes successfully, you will be prompted to configure the job submission. Accept the defaults by clicking on the Submit job button.
- Check the notifications and submission output to confirm that the SPL application was submitted successfully. Take a note of the job ID.
- Right-click on the
- Verify that your Java primitive operator is working as expected.
- Bring up the Streams Explorer and locate the new job in the Instances section.
- Hover over the job and click on the Download Job Logs button.
- Select a folder on your machine where the job logs will be downloaded to.
- Unpackage the job logs
.tar.gz
file and open the PE output filepec.pe.<pe-id>.stdouterr
. This is located inapp-X/jobs/<job-id>
. - Verify that the output shows a series of sum messages.
"1 + 9 = 10" "2 + 8 = 10" "3 + 7 = 10" "4 + 6 = 10" "5 + 5 = 10" "6 + 4 = 10" "7 + 3 = 10" "8 + 2 = 10" "9 + 1 = 10" "10 + 0 = 10"
- After you have verified the result, cancel the running job.
- Bring up the Streams Explorer and locate the job in the Instances section.
- Hover over the job and click on the Cancel Job button.