Search in sources :

Example 61 with Query

use of org.wso2.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery3.

@Test
public void testPartitionQuery3() throws InterruptedException {
    log.info("Partition test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan3");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    Partition partition = Partition.partition().with("cseEventStream", Expression.variable("symbol"));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query.insertIntoInner("StockStream");
    Query query1 = Query.query();
    query1.from(InputStream.innerStream("StockStream"));
    query1.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query1.insertInto("OutStockStream");
    partition.addQuery(query);
    partition.addQuery(query1);
    siddhiApp.addPartition(partition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(4, count.get());
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) 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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 62 with Query

use of org.wso2.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery17.

@Test
public void testPartitionQuery17() throws InterruptedException {
    log.info("Partition test17");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan17");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    Partition partition = Partition.partition().with("cseEventStream", Partition.range("LessValue", Expression.compare(Expression.value(200), Compare.Operator.GREATER_THAN, Expression.variable("volume"))), Partition.range("HighValue", Expression.compare(Expression.value(200), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("volume"))));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(Selector.selector().select("sumvolume", Expression.function("sum", Expression.variable("volume"))));
    query.insertInto("StockStream");
    partition.addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(100L, event.getData()[0]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(600L, event.getData()[0]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(200L, event.getData()[0]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(250L, event.getData()[0]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 600 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 50 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) 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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 63 with Query

use of org.wso2.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery7.

@Test
public void testPartitionQuery7() throws InterruptedException {
    log.info("Partition test7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest7') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream)" + "begin" + "@info(name = 'query') from cseEventStream select symbol,sum(price) as price,volume insert into " + "OutStockStream ;" + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(75.0, event.getData()[1]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(705.0, event.getData()[1]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(110.0, event.getData()[1]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(50.0, event.getData()[1]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75f, 100 });
    inputHandler.send(new Object[] { "WSO2", 705f, 100 });
    inputHandler.send(new Object[] { "IBM", 35f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 50.0f, 100 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 64 with Query

use of org.wso2.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery23.

@Test
public void testPartitionQuery23() throws InterruptedException {
    log.info("Partition test23");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest10') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream)" + "begin" + "@info(name = 'query') from cseEventStream#window.time(1 sec) " + "select symbol, avg(price) as avgPrice, volume " + "having avgPrice >= 0 or avgPrice is null " + "insert expired events into OutStockStream ;" + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(null, event.getData()[1]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75f, 100 });
    SiddhiTestHelper.waitForEvents(200, 1, count, 60000);
    AssertJUnit.assertEquals(1, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 65 with Query

use of org.wso2.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery18.

@Test
public void testPartitionQuery18() throws InterruptedException {
    log.info("Partition test18");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest18') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStreamOne (symbol string, price float,volume int);" + "@info(name = 'query')from cseEventStreamOne select symbol,price,volume insert into cseEventStream;" + "partition with (price>=100 as 'large' or price<100 as 'small' of cseEventStream) begin @info(name " + "= 'query1') from cseEventStream#window.length(4) select symbol,sum(price) as price insert into " + "OutStockStream ;  end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                eventArrived = true;
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(25.0, event.getData()[1]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(7005.60009765625, event.getData()[1]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertTrue(event.getData()[1].equals(75.0) || event.getData()[1].equals(100.0));
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(100.0, event.getData()[1]);
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 25f, 100 });
    inputHandler.send(new Object[] { "WSO2", 7005.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 50f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 25f, 100 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
    AssertJUnit.assertTrue(count.get() <= 4);
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)1150 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1093 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1065 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1006 Event (org.wso2.siddhi.core.event.Event)673 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)528 TestUtil (org.wso2.siddhi.core.TestUtil)300 Query (org.wso2.siddhi.query.api.execution.query.Query)145 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)121 PreparedStatement (java.sql.PreparedStatement)97 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)83 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)82 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)78 SQLException (java.sql.SQLException)73 ArrayList (java.util.ArrayList)73 Connection (java.sql.Connection)69 ResultSet (java.sql.ResultSet)50 HashMap (java.util.HashMap)40 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)20