Search in sources :

Example 76 with Event

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

the class PurgingTestCase method incrementalPurgingTest2.

@Test
public void incrementalPurgingTest2() throws InterruptedException {
    LOG.info("incrementalPurgingTest2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = " define stream stockStream (symbol string, price float, lastClosingPrice float," + " volume long ,quantity int);";
    String query = "  @purge(enable='true',interval='5 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'" + "                ,days='all',months='all',years='all'))  " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate every sec...years ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26 });
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96 });
    Thread.sleep(1000);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
    EventPrinter.print(events);
    AssertJUnit.assertNotNull("Initial queried events cannot be null", events);
    AssertJUnit.assertEquals("Number of events before purging", 6, events.length);
    Thread.sleep(120100);
    events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
    EventPrinter.print(events);
    AssertJUnit.assertNull("No events expected after purging", events);
    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) Test(org.testng.annotations.Test)

Example 77 with Event

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

the class PurgingTestCase method incrementalPurgingTestCase3.

@Test
public void incrementalPurgingTestCase3() throws InterruptedException {
    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='true',interval='10 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'," + "days='all',months='all',years='all'))   " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:51 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289951011L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289952000L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289950000L });
        Thread.sleep(1000);
        stockStreamInputHandler.send(new Object[] { "WSO2", 500f, null, 200L, 7, 1496289951011L });
        Thread.sleep(1000);
        // Thursday, June 1, 2017 4:05:53 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289953000L });
        Thread.sleep(1000);
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289953000L });
        Thread.sleep(2000);
        Event[] events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
        EventPrinter.print(events);
        AssertJUnit.assertEquals("Check time windows", 7, events.length);
        List<Object[]> eventsList = new ArrayList<>();
        for (Event event : events) {
            eventsList.add(event.getData());
        }
        List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 50.0 }, new Object[] { 1496289950000L, "IBM", 900.0 }, new Object[] { 1496289951000L, "IBM", 100.0 }, new Object[] { 1496289951000L, "WSO2", 500.0 }, new Object[] { 1496289952000L, "IBM", 400.0 }, new Object[] { 1496289953000L, "IBM", 100.0 }, new Object[] { 1496289953000L, "WSO2", 100.0 });
        AssertJUnit.assertTrue("Data Matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsList, expected));
        Thread.sleep(120000);
        events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
        EventPrinter.print(events);
        Assert.assertNull(events);
    } finally {
        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 78 with Event

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

the class LatestAggregationTestCase method latestTestCase4.

@Test(dependsOnMethods = "latestTestCase3")
public void latestTestCase4() throws InterruptedException {
    LOG.info("latestTestCase4: testing latest incremental aggregator - different 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, avg(price) as avgPrice, (price * quantity) as latestPrice " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select s.symbol, s.latestPrice, sum(s.avgPrice) as totalAvg " + "group by s.symbol " + "order by AGG_TIMESTAMP " + "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, 1496289950010L });
        stockStreamInputHandler.send(new Object[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952010L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952020L });
        // Thursday, June 1, 2017 4:05:50 AM - Out of order event
        stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954010L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954020L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956010L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956030L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO22", 750f, 65.0 }, new Object[] { "WSO24", 1600f, 80.0 }, new Object[] { "IBM1", 3500f, 801.5 });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 10000);
        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 79 with Event

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

the class LatestAggregationTestCase method latestTestCase3.

@Test(dependsOnMethods = "latestTestCase2")
public void latestTestCase3() throws InterruptedException {
    LOG.info("latestTestCase3: testing latest incremental aggregator with another agg");
    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, avg(price) as avgPrice, (price * quantity) as latestPrice " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select AGG_TIMESTAMP, s.symbol, s.latestPrice, s.avgPrice " + "order by AGG_TIMESTAMP " + "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;
                }
                if (removeEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : removeEvents) {
                        removeEventsList.add(event.getData());
                        removeEventCount.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[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:50 AM - Out of order event
        stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO22", 750f, 65.0 }, new Object[] { 1496289952000L, "WSO24", 1600f, 80.0 }, new Object[] { 1496289954000L, "IBM1", 10200f, 101.5 }, new Object[] { 1496289956000L, "IBM1", 3500f, 700.0 });
        SiddhiTestHelper.waitForEvents(100, 4, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 4, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of remove events", 4, removeEventCount.get());
        AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isUnsortedEventsMatch(removeEventsList, 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 80 with Event

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

the class LatestAggregationTestCase method latestTestCase2.

@Test(dependsOnMethods = "latestTestCase1")
public void latestTestCase2() throws InterruptedException {
    LOG.info("latestTestCase2: testing latest incremental aggregator - different 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, avg(price) as avgPrice, (price * quantity) as latestPrice " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select s.symbol, s.latestPrice " + "group by s.symbol " + "order by AGG_TIMESTAMP " + "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;
                }
                if (removeEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : removeEvents) {
                        removeEventsList.add(event.getData());
                        removeEventCount.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[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:50 AM - Out of order event
        stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO22", 750f }, new Object[] { "WSO24", 1600f }, new Object[] { "IBM1", 3500f });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of remove events", 3, removeEventCount.get());
        AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isUnsortedEventsMatch(removeEventsList, 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