Search in sources :

Example 71 with Event

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

the class QueryAPITestCaseForTestStore method test0.

@Test
public void test0() throws InterruptedException {
    log.info("Test1 table with cache");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 200L });
    Thread.sleep(500);
    Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    events = siddhiAppRuntime.query("" + "from StockTable " + "on price > 75 ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    events = siddhiAppRuntime.query("" + "from StockTable " + "on price > volume*3/4  ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    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 72 with Event

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

the class QueryAPITestCaseForTestStore method test1.

@Test
public void test1() throws InterruptedException {
    log.info("Test1 table with cache");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO3", 57.6f, 100L });
    Thread.sleep(500);
    Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    events = siddhiAppRuntime.query("" + "from StockTable " + "on price > 75 ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    events = siddhiAppRuntime.query("" + "from StockTable " + "on price > volume*3/4  ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    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 73 with Event

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

the class QueryAPITestCaseForTestStore method test10.

@Test
public void test10() throws InterruptedException {
    log.info("Test10 table with cache");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    Thread.sleep(500);
    String storeQuery = "" + "from StockTable " + "on volume > 10 " + "select symbol, sum(volume) as totalVolume " + "group by symbol " + "having symbol == 'WSO2'";
    Event[] events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[1]);
    events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[1]);
}
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 74 with Event

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

the class UpdateTestStoreTestCase method updateFromTableTest6.

@Test
public void updateFromTableTest6() throws InterruptedException, SQLException {
    log.info("updateFromTableTest6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream[(StockTable.symbol==symbol) in StockTable] " + "insert into OutStream;";
    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++;
                }
                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", 55.6f, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L });
    checkStockStream.send(new Object[] { "WSO2", 100L });
    checkStockStream.send(new Object[] { "IBM", 100L });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Number of success events", 3, inEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    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 75 with Event

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

the class DistinctCountAggregationTestCase method incrementalStreamProcessorTest1.

@Test
public void incrementalStreamProcessorTest1() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest1: testing distinctCount incremental aggregator");
    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 distinctCount(symbol) as distinctCnt " + "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 \"days\" " + "select AGG_TIMESTAMP, s.distinctCnt " + "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", 70f, null, 40L, 10, 1496289950000L });
        // 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:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, 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[] { "IBM1", 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[] { "IBM2", 600f, null, 200L, 6, 1496290076000L });
        // Thursday, June 1, 2017 5:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
        // Thursday, June 1, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 61f, 44f, 200L, 56, 1496297276000L });
        // Friday, June 2, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 801f, null, 100L, 10, 1496383676000L });
        // Saturday, June 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 901f, null, 100L, 15, 1496470076000L });
        // Monday, July 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 96, 1499062076000L });
        // Thursday, August 3, 2017 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 402f, null, 200L, 9, 1501740476000L });
        // Friday, August 3, 2018 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 63f, 44f, 200L, 6, 1533276476000L });
        // Saturday, August 3, 2019 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 260f, 44f, 200L, 16, 1564812476000L });
        // Monday, August 3, 2020 6:07:56 AM
        stockStreamInputHandler.send(new Object[] { "CISCO", 26f, 44f, 200L, 16, 1596434876000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        List<Object[]> expected = Arrays.asList(new Object[] { 1496275200000L, 8 }, new Object[] { 1496361600000L, 1 }, new Object[] { 1496448000000L, 1 }, new Object[] { 1499040000000L, 1 }, new Object[] { 1501718400000L, 1 }, new Object[] { 1533254400000L, 1 }, new Object[] { 1564790400000L, 1 }, new Object[] { 1596412800000L, 1 });
        SiddhiTestHelper.waitForEvents(100, 8, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 8, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of remove events", 8, removeEventCount.get());
        AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isEventsMatch(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