Search in sources :

Example 81 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase6.

@Test(dependsOnMethods = "aggregationFunctionTestcase5")
public void aggregationFunctionTestcase6() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase2 - count with different group by");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, name string, price float, lastClosingPrice float, " + "volume long , quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, name, count() as count " + "group by symbol, name " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select s.symbol, sum(count) as count " + "group by s.symbol " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 100f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 100f, null, 200L, 96, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 500f, null, 200L, 7, 1496289956000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 400f, null, 200L, 9, 1496290016000L });
        // Thursday, June 1, 2017 4:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", "CISCO1", 700f, null, 200L, 20, 1496293676000L });
        Thread.sleep(2000);
        inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", 4L }, new Object[] { "IBM", 6L }, new Object[] { "CISCO", 1L });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 82 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase15.

@Test(dependsOnMethods = { "aggregationFunctionTestcase13" })
public void aggregationFunctionTestcase15() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase15 - Group by AGG_TIMESTAMP");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "@purge(enable='false')" + " define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    // 1 June 2017 04:05:50
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 70f, null, 40L, 10, 1496289950000L });
    // 1 June 2017 04:05:52
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289952500L });
    // 1 June 2017 04:05:54
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289954500L });
    Thread.sleep(2000);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-06-** **:**:**\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, sum(totalPrice) as totalPrice " + "group by AGG_TIMESTAMP;");
    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(3, events.length);
    List<Object[]> eventsOutputList = new ArrayList<>();
    for (Event event : events) {
        eventsOutputList.add(event.getData());
    }
    List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, 120.0 }, new Object[] { 1496289952000L, 160.0 }, new Object[] { 1496289954000L, 200.0 });
    AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsOutputList, expected));
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 83 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase4.

@Test(dependsOnMethods = "aggregationFunctionTestcase3")
public void aggregationFunctionTestcase4() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase2 - count with same group by");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, count() as count " + "group by symbol " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select s.symbol, sum(count) as count " + "group by s.symbol " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
        // Thursday, June 1, 2017 4:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
        Thread.sleep(2000);
        inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", 4L }, new Object[] { "IBM", 6L }, new Object[] { "CISCO", 1L });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 84 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase3.

@Test(dependsOnMethods = "aggregationFunctionTestcase2")
public void aggregationFunctionTestcase3() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase2 - count w/o group by");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, count() as count " + "group by symbol " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select AGG_TIMESTAMP, s.symbol, s.count " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
        // Thursday, June 1, 2017 4:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
        Thread.sleep(2000);
        inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 2L }, new Object[] { 1496289952000L, "WSO2", 2L }, new Object[] { 1496289954000L, "IBM", 2L }, new Object[] { 1496289956000L, "IBM", 2L }, new Object[] { 1496290016000L, "IBM", 1L }, new Object[] { 1496290076000L, "IBM", 1L }, new Object[] { 1496293676000L, "CISCO", 1L });
        SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 85 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase7.

@Test(dependsOnMethods = "aggregationFunctionTestcase6")
public void aggregationFunctionTestcase7() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase7 - count with different group by");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, name string, price float, lastClosingPrice float, " + "volume long , quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, name, count() as count " + "group by symbol, name " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select s.symbol, s.name, sum(count) as count " + "group by s.symbol " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 100f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 100f, null, 200L, 96, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 500f, null, 200L, 7, 1496289956000L });
        // Thursday, June 1, 2017 4:06:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 400f, null, 200L, 9, 1496290016000L });
        // Thursday, June 1, 2017 4:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", "CISCO1", 700f, null, 200L, 20, 1496293676000L });
        Thread.sleep(2000);
        inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", "WSO22", 4L }, new Object[] { "IBM", "IBM1", 6L }, new Object[] { "CISCO", "CISCO1", 1L });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Event (io.siddhi.core.event.Event)1298 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1257 SiddhiManager (io.siddhi.core.SiddhiManager)1255 Test (org.testng.annotations.Test)1251 InputHandler (io.siddhi.core.stream.input.InputHandler)1216 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)818 StreamCallback (io.siddhi.core.stream.output.StreamCallback)303 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (io.siddhi.query.api.SiddhiApp)82 Query (io.siddhi.query.api.execution.query.Query)81 ArrayList (java.util.ArrayList)80 Logger (org.apache.logging.log4j.core.Logger)52 StreamEvent (io.siddhi.core.event.stream.StreamEvent)38 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 ComplexEvent (io.siddhi.core.event.ComplexEvent)29 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)29 UnitTestAppender (io.siddhi.core.UnitTestAppender)18 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)17 HashMap (java.util.HashMap)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)14