Siddhi 4.0.0 Early Access

Siddhi 4.0.0 is being developed using Java 8 with major core level changes and features. One of the fundamental change to note is that some features of Siddhi have been moved as external extensions to Siddhi and WSO2 Complex Event Processor. This tutorial shows you, how to migrate the complex event project developed using Siddhi 3.x to Siddhi 4.x. Take the sample project developed in "Complex Event Processing - An Introduction" article which was developed using Siddhi 3.1.0.

Warning: Still, Siddhi 4.0.0 is not production ready and it is not recommended to use it until officially released by WSO2.

Required Tools:
  • Git
  • Apache Maven
  • Java 1.8
  • Eclipse or IntelliJ IDEA

Since Siddhi 4.0.0 is not officially released yet, we are going to compile the Siddhi 4.0.0-SNAPSHOT version manually.

Step 1:
Clone the Siddhi project using the following command
git clone https://github.com/wso2/siddhi.git

Step 2:
Set the following environment variable to avoid the Maven OutOfMemoryError.
MAVEN_OPTS="-Xms768m -Xmx3072m -XX:MaxPermSize=1200m"

Step 3:
Open the terminal in the siddhi directory and execute the following command to build Siddhi.
mvn clean install

You can reduce the time to build by skipping the tests using the following command
mvn clean install -Dmaven.test.skip=true

The external unique time batch window used in the "Complex Event Processing - An Introduction" is no more available in the Siddhi core. Most of the features are moved to a separate GitHub account known as "wso2-extensions". The external unique time batch window is available under the siddhi-execution-unique. Follow the steps to clone and build the unique extension.

Step 1:
Clone the siddhi-execution-unique project using the following command.
git clone https://github.com/wso2-extensions/siddhi-execution-unique.git

Step 2:
Open the terminal in the siddhi-execution-unique directory and execute the following command to build Siddhi.
mvn clean install

You can reduce the time to build by skipping the tests using the following command
mvn clean install -Dmaven.test.skip=true

Now clone the siddhidemo project which is from the Complex Event Processing - An Introduction" article or create a new project similar to that as suggested in that article.

Step 1:
Create a new Maven project in Eclipse using the following information.
Group Id: com.javahelps
Artifact Id: siddhimigrationdemo

Step 2:
Add the following dependencies to the pom.xml. Compared to Siddhi 3.x project, siddhi-execution-unique is required in addition.

Note: Check the pom file of Siddhi and siddhi-execution-unique projects and use the same versions in the following properties tag.
<properties>
    <siddhi.version>4.0.0-SNAPSHOT</siddhi.version>
    <siddhi.execution.unique.version>3.1.3-SNAPSHOT</siddhi.execution.unique.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <!--Siddhi -->
    <dependency>
        <groupId>org.wso2.siddhi</groupId>
        <artifactId>siddhi-query-api</artifactId>
        <version>${siddhi.version}</version>
    </dependency>
    <dependency>
        <groupId>org.wso2.siddhi</groupId>
        <artifactId>siddhi-core</artifactId>
        <version>${siddhi.version}</version>
    </dependency>
    <dependency>
        <groupId>org.wso2.siddhi</groupId>
        <artifactId>siddhi-query-compiler</artifactId>
        <version>${siddhi.version}</version>
    </dependency>
    <dependency>
        <groupId>org.wso2.extension.siddhi.execution.unique</groupId>
        <artifactId>siddhi-execution-unique</artifactId>
        <version>${siddhi.execution.unique.version}</version>
    </dependency>
</dependencies>

Step 3:
Create a new package com.javahelps.siddhimigrationdemo.

Step 4:
Create the Main.java as shown below. If you compare this class with the one used in Siddhi 3.x project, the way to use external time batch window is different. In Siddhi 3.x, the external time batch window is referred as #window.uniqueExternalTimeBatch but based on Siddhi 4.0.0 and the unique extension, it must be referred as #window.unique:externalTimeBatch.
package com.javahelps.siddhimigrationdemo;

import java.util.Random;

import org.wso2.siddhi.core.ExecutionPlanRuntime;
import org.wso2.siddhi.core.SiddhiManager;
import org.wso2.siddhi.core.event.Event;
import org.wso2.siddhi.core.stream.input.InputHandler;
import org.wso2.siddhi.core.stream.output.StreamCallback;

public class Main {
    //    SiddhiDe
    private static final String[] CUSTOMERS = {"Alice", "Barby", "Carol", "Diana"};
    private static final String[] ITEMS = {"Cocoa-Butter Lotion", "Purse-XL", "Purse-L", "Beer", "Biscuit",
            "Chocolate", "ZMA"};
    private static final Random RANDOM = new Random();

    public static void main(String[] args) throws InterruptedException {
        SiddhiManager siddhiManager = new SiddhiManager();

        String streams = "define stream purchaseStream (customerName string, item string, timestamp long); ";
        String query = "partition with (customerName of purchaseStream) " +
                "begin " +
                "from purchaseStream[item == 'Cocoa-Butter Lotion' OR item == 'Purse-XL' OR item == 'ZMA']#window" +
                ".unique:externalTimeBatch(item, timestamp, 500 milliseconds) " +
                "select customerName, convert(count(item), 'double') / 3.0 * 100.0 as noOfPurchases insert into " +
                "possiblePregnant; " +
                "end ";

        // Create ExecutionPlanRuntime using stream definition and query
        ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query);

        try {
            // Receive the output events
            executionPlanRuntime.addCallback("possiblePregnant", new StreamCallback() {
                @Override
                public void receive(Event[] events) {
                    String output = String.format("\t\t\t%s is pregnant with %.2f%% confidence.", events[0].getData(0),
                            events[0].getData(1));
                    System.out.println(output);
                }
            });

            // Send input events
            InputHandler purchaseStream = executionPlanRuntime.getInputHandler("purchaseStream");
            executionPlanRuntime.start();
            for (int i = 0; i < 1000; i++) {
                Object[] event = generateEvent();
                purchaseStream.send(event);
                Thread.sleep(10); // Delay for 10 milliseconds
            }
        } finally {
            executionPlanRuntime.shutdown();
        }
    }

    private static Object[] generateEvent() {
        String name = CUSTOMERS[RANDOM.nextInt(CUSTOMERS.length)];
        String item = ITEMS[RANDOM.nextInt(ITEMS.length)];
        long time = System.currentTimeMillis(); // Current time

        System.out.println(name + " buys " + item);
        Object[] event = new Object[]{name, item, time};
        return event;
    }

}

Save all the changes and run the Main.java. You will see the same results as the Siddhi 3.x project.

Find the project @ GitHub.
Previous
Next Post »

Contact Form

Name

Email *

Message *