Search in sources :

Example 16 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class JoinTestCase method joinTest15.

@Test
public void joinTest15() throws InterruptedException {
    log.info("Join test15");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream order (billnum string, custid string, items string, dow string, timestamp long); " + "define table dow_items (custid string, dow string, item string) ; " + "define stream dow_items_stream (custid string, dow string, item string); ";
    String query = "" + "@info(name = 'query1') " + "from order join dow_items \n" + "on order.custid == dow_items.custid \n" + "select  dow_items.item\n" + "having dow_items.item == \"item1\" \n" + "insert into recommendationStream ;" + "@info(name = 'query2') " + "from dow_items_stream " + "insert into dow_items ;" + "" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler orderStream = siddhiAppRuntime.getInputHandler("order");
        InputHandler itemsStream = siddhiAppRuntime.getInputHandler("dow_items_stream");
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                eventArrived = true;
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(100);
        itemsStream.send(new Object[] { "cust1", "bill1", "item1" });
        orderStream.send(new Object[] { "bill1", "cust1", "item1", "dow1", 12323232L });
        Thread.sleep(100);
        AssertJUnit.assertEquals("Event Arrived", true, eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 17 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class JoinTestCase method joinTest20.

@Test
public void joinTest20() throws InterruptedException {
    log.info("Join test20");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream dataIn (id int, data string); " + "define stream countIn (id int); " + "define stream deleteIn (id int); " + "define table dataTable (id int, data string); ";
    String query = "" + "from dataIn\n" + "insert into dataTable;\n" + "" + "from deleteIn \n" + "delete dataTable\n" + "    on dataTable.id == id;\n" + "" + "@info(name = 'query1') " + "from countIn as c left outer join dataTable as d\n" + "on d.data == 'abc'\n" + "select count() as count\n" + "insert into countOut; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler dataIn = siddhiAppRuntime.getInputHandler("dataIn");
        InputHandler countIn = siddhiAppRuntime.getInputHandler("countIn");
        InputHandler deleteIn = siddhiAppRuntime.getInputHandler("deleteIn");
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                inEventCount.incrementAndGet();
                if (inEventCount.get() == 1) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(0) == 3L);
                } else {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(0) == 2L);
                }
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(100);
        dataIn.send(new Object[] { 1, "abc" });
        dataIn.send(new Object[] { 2, "abc" });
        dataIn.send(new Object[] { 3, "abc" });
        countIn.send(new Object[] { 1 });
        deleteIn.send(new Object[] { 1 });
        countIn.send(new Object[] { 1 });
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 18 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class JoinTestCase method joinTest16.

@Test
public void joinTest16() throws InterruptedException {
    log.info("Join test16");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream order (billnum string, custid string, items string, dow string, timestamp long); " + "define table dow_items (custid string, dow string, item string) ; " + "define stream dow_items_stream (custid string, dow string, item string); ";
    String query = "" + "@info(name = 'query1') " + "from order join dow_items \n" + "on order.custid == dow_items.custid \n" + "select  order.custid\n" + "having dow_items.item == \"item1\" \n" + "insert into recommendationStream ;" + "@info(name = 'query2') " + "from dow_items_stream " + "insert into dow_items ;" + "" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler orderStream = siddhiAppRuntime.getInputHandler("order");
        InputHandler itemsStream = siddhiAppRuntime.getInputHandler("dow_items_stream");
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                eventArrived = true;
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(100);
        itemsStream.send(new Object[] { "cust1", "bill1", "item1" });
        orderStream.send(new Object[] { "bill1", "cust1", "item1", "dow1", 12323232L });
        Thread.sleep(100);
        AssertJUnit.assertEquals("Event Arrived", true, eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 19 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class OuterJoinTestCase method joinTest1.

@Test
public void joinTest1() throws InterruptedException {
    log.info("Outer Join test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "@info(name = 'query1') " + "from cseEventStream#window.length(3) full outer join twitterStream#window.length(1) " + "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);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                inEventCount++;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals("WSO2", inEvent.getData(0));
                    AssertJUnit.assertEquals(null, inEvent.getData(1));
                    AssertJUnit.assertEquals(55.6f, inEvent.getData(2));
                }
                if (inEventCount == 2) {
                    AssertJUnit.assertEquals("WSO2", inEvent.getData(0));
                    AssertJUnit.assertEquals("Hello World", inEvent.getData(1));
                    AssertJUnit.assertEquals(55.6f, inEvent.getData(2));
                }
                if (inEventCount == 3) {
                    AssertJUnit.assertEquals("IBM", inEvent.getData(0));
                    AssertJUnit.assertEquals(null, inEvent.getData(1));
                    AssertJUnit.assertEquals(75.6f, inEvent.getData(2));
                }
                if (inEventCount == 4) {
                    AssertJUnit.assertEquals("WSO2", inEvent.getData(0));
                    AssertJUnit.assertEquals("Hello World", inEvent.getData(1));
                    AssertJUnit.assertEquals(57.6f, inEvent.getData(2));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
    siddhiAppRuntime.start();
    cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
    cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
    cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(500);
    AssertJUnit.assertEquals(4, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 20 with QueryCallback

use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class FilterTestCase2 method testFilterQuery112.

// ****************************************************************************************************************
// Expression Multiply
@Test
public void testFilterQuery112() throws InterruptedException {
    log.info("Filter test112");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("multipliedQuantity", Expression.multiply(Expression.variable("quantity"), Expression.value(4))).select("multipliedPrice", Expression.multiply(Expression.variable("price"), Expression.value(2))).select("multipliedVolume", Expression.multiply(Expression.variable("volume"), Expression.value(3))).select("multipliedAwards", Expression.multiply(Expression.variable("awards"), Expression.value(5))));
    query.insertInto("OutputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("111.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("300.0".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("15".equals(inEvents[0].getData()[4].toString()));
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 3L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiManager (org.wso2.siddhi.core.SiddhiManager)590 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)590 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)588 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)585 Event (org.wso2.siddhi.core.event.Event)584 Test (org.testng.annotations.Test)582 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)73 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)73 Query (org.wso2.siddhi.query.api.execution.query.Query)73 ArrayList (java.util.ArrayList)25 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)12 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)11 Test (org.junit.Test)5 ExecutionPlanRuntime (org.wso2.siddhi.core.ExecutionPlanRuntime)5 StringConcatAggregatorString (org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1