Search in sources :

Example 91 with Event

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

the class Aggregation1TestCase method incrementalStreamProcessorTest35.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest34" })
public void incrementalStreamProcessorTest35() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest35");
    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, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 26, 1513578087000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 96, 1513578087000L });
    Thread.sleep(2000);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 11:51:27 +05:30\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertArrayEquals(new Object[] { 1513578087000L, "CISCO", 100.0, 200.0, 9600f }, events[0].getData());
    Thread.sleep(100);
    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 92 with Event

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

the class Aggregation1TestCase method incrementalStreamProcessorTest11.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest9" })
public void incrementalStreamProcessorTest11() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest11");
    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, sum(price) as totalPrice, " + "(price * quantity) as lastTradeValue  " + "group by symbol " + "aggregate every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    Thread.sleep(2000);
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    Thread.sleep(2000);
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    Thread.sleep(5000);
    LocalDate currentDate = LocalDate.now();
    String year = String.valueOf(currentDate.getYear());
    String month = String.valueOf(currentDate.getMonth().getValue());
    if (month.length() == 1) {
        month = "0".concat(month);
    }
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "on symbol == \"IBM\" " + "within \"" + year + "-" + month + "-** **:**:** +05:30\" " + "per \"seconds\"; ");
    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);
    Object[] copyEventsWithoutTime = new Object[4];
    System.arraycopy(events[0].getData(), 1, copyEventsWithoutTime, 0, 4);
    AssertJUnit.assertArrayEquals(new Object[] { "IBM", 100.0, 200.0, 9600f }, copyEventsWithoutTime);
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) LocalDate(java.time.LocalDate) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 93 with Event

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

the class Aggregation1TestCase method incrementalStreamProcessorTest34.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest33" })
public void incrementalStreamProcessorTest34() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest34");
    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, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();
    stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 26, 1513578087000L });
    stockStreamInputHandler.send(new Object[] { "CISCO", 100f, null, 200L, 96, 1513578087000L });
    Thread.sleep(2000);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-12-18 06:21:**\" " + "per \"seconds\" " + "select * " + "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertArrayEquals(new Object[] { 1513578087000L, "CISCO", 100.0, 200.0, 9600f }, events[0].getData());
    Thread.sleep(100);
    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 94 with Event

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

the class Aggregation1TestCase method incrementalStreamProcessorTest28.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest27" }, expectedExceptions = StoreQueryCreationException.class)
public void incrementalStreamProcessorTest28() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest28");
    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, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue  " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    siddhiAppRuntime.start();
    Thread.sleep(100);
    Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-06-02 00:00:00\", \"2017-06-01 00:00:00\" " + "per \"hours\"");
    EventPrinter.print(events);
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 95 with Event

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

the class Aggregation1TestCase method incrementalStreamProcessorTest41.

@Test(dependsOnMethods = { "incrementalStreamProcessorTest40" })
public void incrementalStreamProcessorTest41() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest41");
    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 avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) as " + "lastTradeValue, " + "count() as count " + "aggregate by timestamp every sec...year ;" + "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 \"2017-06-01 04:05:**\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, s.avgPrice, totalPrice, lastTradeValue, count " + "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[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
        // Thursday, June 1, 2017 4:05:49 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289949000L });
        stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289949000L });
        // Thursday, June 1, 2017 4:05:48 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289948000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289948000L });
        // Thursday, June 1, 2017 4:05:47 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289947000L });
        stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289947000L });
        // Thursday, June 1, 2017 4:05:46 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289946000L });
        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[] { 1496289946000L, 400.0, 400.0, 3600f, 1L }, new Object[] { 1496289947000L, 700.0, 1400.0, 3500f, 2L }, new Object[] { 1496289948000L, 100.0, 200.0, 9600f, 2L }, new Object[] { 1496289949000L, 80.0, 160.0, 1600f, 2L }, new Object[] { 1496289950000L, 60.0, 120.0, 700f, 2L });
        SiddhiTestHelper.waitForEvents(100, 5, inEventCount, 60000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 5, 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