Search in sources :

Example 91 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method externalTimeBatchWindowTest10.

@Test
public void externalTimeBatchWindowTest10() throws InterruptedException {
    log.info("externalTimeBatchWindow test10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string) ;";
    String query = "" + "@info(name = 'query1') " + "from LoginEvents#window.externalTimeBatch(timestamp, 1 sec, 0, 2 sec) " + "select timestamp, ip, count() as total  " + "insert into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            for (Event event : inEvents) {
                inEventCount++;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(4L, event.getData(2));
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(3L, event.getData(2));
                } else if (inEventCount == 3) {
                    AssertJUnit.assertEquals(5L, event.getData(2));
                } else if (inEventCount == 4) {
                    AssertJUnit.assertEquals(7L, event.getData(2));
                } else if (inEventCount == 5) {
                    AssertJUnit.assertEquals(2L, event.getData(2));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.91" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.92" });
    inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
    inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
    Thread.sleep(3000);
    org.testng.AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    org.testng.AssertJUnit.assertEquals("In Events ", 5, inEventCount);
    org.testng.AssertJUnit.assertEquals("Remove Events ", 0, removeEventCount);
    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 QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method externalTimeBatchWindowTest12.

@Test
public void externalTimeBatchWindowTest12() 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); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.externalTimeBatch(timestamp, 1 sec, 0) join twitterStream#window" + ".externalTimeBatch(timestamp, 1 sec, 0) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", 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);
        org.testng.AssertJUnit.assertEquals(2, inEventCount);
        org.testng.AssertJUnit.assertEquals(0, removeEventCount);
        org.testng.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) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 93 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method externalTimeBatchWindowTest11.

@Test
public void externalTimeBatchWindowTest11() throws InterruptedException {
    log.info("externalTimeBatchWindow test11");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string) ;";
    String query = "" + "@info(name = 'query1') " + "from LoginEvents#window.externalTimeBatch(timestamp, 1 sec, 0) " + "select timestamp, ip, count() as total  " + "insert into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            for (Event event : inEvents) {
                inEventCount++;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(4L, event.getData(2));
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(7L, event.getData(2));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
    Thread.sleep(2100);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.91" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.92" });
    inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
    inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
    Thread.sleep(3000);
    org.testng.AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    org.testng.AssertJUnit.assertEquals("In Events ", 2, inEventCount);
    org.testng.AssertJUnit.assertEquals("Remove Events ", 0, removeEventCount);
    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 94 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method externalTimeBatchWindowTest7.

@Test
public void externalTimeBatchWindowTest7() throws InterruptedException {
    log.info("externalTimeBatchWindow test7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string) ;";
    String query = "" + "@info(name = 'query1') " + "from LoginEvents#window.externalTimeBatch(timestamp, 1 sec, 0, 2 sec) " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(3000);
    inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
    inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
    Thread.sleep(3000);
    inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
    inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
    Thread.sleep(3000);
    org.testng.AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    org.testng.AssertJUnit.assertEquals("In Events ", 4, inEventCount);
    org.testng.AssertJUnit.assertEquals("Remove Events ", 0, removeEventCount);
    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 QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method externalTimeBatchWindowTest13.

@Test
public void externalTimeBatchWindowTest13() throws InterruptedException {
    log.info("externalTimeBatchWindow Test13");
    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); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.externalTimeBatch(timestamp, 1 sec, 0) join twitterStream#window" + ".externalTimeBatch(timestamp, 1 sec, 0) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", 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);
        org.testng.AssertJUnit.assertEquals(2, inEventCount);
        org.testng.AssertJUnit.assertEquals(1, removeEventCount);
        org.testng.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) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)575 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)574 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)574 Test (org.testng.annotations.Test)574 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)572 Event (org.ballerinalang.siddhi.core.event.Event)569 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)73 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)73 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)73 ArrayList (java.util.ArrayList)25 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)11 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)11 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)10 StringConcatAggregatorString (org.ballerinalang.siddhi.core.query.extension.util.StringConcatAggregatorString)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)4 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)1