Search in sources :

Example 91 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class CustomJoinWindowTestCase method testJoinWindowWithTable.

@Test
public void testJoinWindowWithTable() throws InterruptedException {
    log.info("Test join window with table");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string); " + "define window CheckStockWindow(symbol string) length(1) output all events; " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query0') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query1') " + "from CheckStockStream " + "insert into CheckStockWindow ;" + "" + "@info(name = 'query2') " + "from CheckStockWindow join StockTable " + " on CheckStockWindow.symbol==StockTable.symbol " + "select CheckStockWindow.symbol as checkSymbol, StockTable.symbol as symbol, StockTable.volume as " + "volume  " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            assertArrayEquals(new Object[] { "WSO2", "WSO2", 100L }, event.getData());
                            break;
                        default:
                            org.testng.AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 10L });
    checkStockStream.send(new Object[] { "WSO2" });
    Thread.sleep(500);
    assertEquals("Number of success events", 1, inEventCount);
    assertEquals("Number of remove events", 0, removeEventCount);
    assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 92 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class CustomJoinWindowTestCase method testUnidirectionalJoin.

@Test
public void testUnidirectionalJoin() throws InterruptedException {
    log.info("Test unidirectional join with two windows");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); " + "define window cseEventWindow (symbol string, price float, volume int) lengthBatch(2); " + "define window twitterWindow (user string, tweet string, company string) lengthBatch(2); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from twitterStream " + "insert into twitterWindow; " + "" + "@info(name = 'query2') " + "from cseEventWindow unidirectional join twitterWindow " + "on cseEventWindow.symbol == twitterWindow.company " + "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("cseEventWindow", new StreamCallback() {

            @Override
            public void receive(Event[] events) {
                System.out.print("cseEventWindow: ");
                EventPrinter.print(events);
            }
        });
        siddhiAppRuntime.addCallback("twitterWindow", new StreamCallback() {

            @Override
            public void receive(Event[] events) {
                System.out.print("twitterWindow: ");
                EventPrinter.print(events);
            }
        });
        siddhiAppRuntime.addCallback("query2", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        cseEventStreamHandler.send(new Object[] { "IBM", 59.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        twitterStreamHandler.send(new Object[] { "User2", "Hello World2", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(500);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertEquals(2, inEventCount);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 93 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class CustomJoinWindowTestCase method testGroupByUseCase.

@Test
public void testGroupByUseCase() throws InterruptedException {
    log.info("Test joining a single window by multiple streams");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream SensorStream (name string, value float, roomNo int, deviceID string); " + "define window SensorWindow (name string, value float, roomNo int, deviceID string) timeBatch(1 " + "second); ";
    String query = "" + "@info(name = 'query0') " + "from SensorStream " + "insert into SensorWindow; " + "@info(name = 'query1') " + "from SensorWindow  " + "select name, max(value) as maxValue, roomNo " + "group by name, roomNo " + "insert into MaxSensorReadingPerRoomStream; " + "@info(name = 'query2') " + "from SensorWindow  " + "select name, max(value) as maxValue " + "group by name " + "insert into OverallMaxSensorReadingStream; " + "@info(name = 'query3') " + "from SensorWindow  " + "select name, avg(value) as avgValue " + "group by name " + "insert into OverallAverageSensorReadingStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("MaxSensorReadingPerRoomStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("MaxSensorReadingPerRoomStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallMaxSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallMaxSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallAverageSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallAverageSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.start();
    InputHandler sensorStreamInputHandler = siddhiAppRuntime.getInputHandler("SensorStream");
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.0f, 1, "T001A" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 20.0f, 1, "P001" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 25.0f, 2, "T002" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 24.0f, 3, "T003" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 45.0f, 3, "P003" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.5f, 1, "T001B" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 15.0f, 2, "H002" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 10.0f, 3, "H003" });
    Thread.sleep(1000);
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 94 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow12.

@Test
public void testExternalTimeBatchWindow12() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (timestamp long, symbol string, price float, volume int); " + "define stream twitterStream (timestamp long, user string, tweet string, company string); " + "define window cseEventWindow (timestamp long, symbol string, price float, volume int) " + "externalTimeBatch(timestamp, 1 sec, 0); " + "define window twitterWindow (timestamp long, user string, tweet string, company string) " + "externalTimeBatch(timestamp, 1 sec, 0); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from twitterStream " + "insert into twitterWindow; " + "" + "@info(name = 'query2') " + "from cseEventWindow join twitterWindow " + "on cseEventWindow.symbol== twitterWindow.company " + "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", new QueryCallback() {

            @Override
            public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timeStamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { 1366335804341L, "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { 1366335804341L, "User1", "Hello World", "WSO2" });
        twitterStreamHandler.send(new Object[] { 1366335805301L, "User2", "Hello World2", "WSO2" });
        cseEventStreamHandler.send(new Object[] { 1366335805341L, "WSO2", 75.6f, 100 });
        cseEventStreamHandler.send(new Object[] { 1366335806541L, "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        assertEquals(2, inEventCount);
        assertEquals(0, removeEventCount);
        assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 95 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow9.

@Test
public void testExternalTimeBatchWindow9() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test9");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 2 sec); ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "group by ip " + "insert into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {

        @Override
        public synchronized void receive(Event[] events) {
            if (events != null) {
                inEventCount = inEventCount + events.length;
                for (Event event : events) {
                    sum = sum + (Long) event.getData(2);
                }
            }
            eventArrived = true;
        }
    });
    final InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            int i = 0;
            long time = 1366335804341L;
            while (i < 10000) {
                try {
                    inputHandler.send(new Object[] { time, "192.10.1." + Thread.currentThread().getId() });
                } catch (InterruptedException e) {
                    log.error(e.getMessage(), e);
                }
                time += 1000;
                i++;
            }
        }
    };
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    Thread.sleep(10000);
    assertEquals(10 * 10000, sum);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1235 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1231 Test (org.testng.annotations.Test)1231 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1123 Event (org.ballerinalang.siddhi.core.event.Event)798 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)574 TestUtil (org.ballerinalang.siddhi.core.TestUtil)300 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)85 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)80 ArrayList (java.util.ArrayList)25 InMemoryBroker (org.ballerinalang.siddhi.core.util.transport.InMemoryBroker)16 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)12 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)12 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)12 HashMap (java.util.HashMap)11 InMemoryConfigManager (org.ballerinalang.siddhi.core.util.config.InMemoryConfigManager)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6