Search in sources :

Example 21 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class JoinPartitionTestCase method testJoinPartition7.

@Test
public void testJoinPartition7() throws InterruptedException {
    log.info("Join partition test7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "define stream cseEventStream (symbol string, user string,volume int);  define stream " + "twitterStream (user string, tweet string, company string, volume int);" + "partition with (volume>=100 as 'large' or volume<100 as 'small' of cseEventStream, volume>=100 as " + "'large' or volume<100 as 'small' of twitterStream) begin @info(name = 'query1') " + "from cseEventStream#window.time(1 sec) join twitterStream#window.time(1 sec) " + "on cseEventStream.user== twitterStream.user " + "select cseEventStream.symbol as symbol, cseEventStream.user as user,twitterStream.tweet, " + "cseEventStream.volume,twitterStream.company " + "insert all events into outputStream;" + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
    siddhiAppRuntime.start();
    cseEventStreamHandler.send(new Object[] { "WSO2", "User1", 200 });
    twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2", 200 });
    twitterStreamHandler.send(new Object[] { "User1", "World", "WSO2", 200 });
    cseEventStreamHandler.send(new Object[] { "IBM", "User1", 10 });
    twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2", 10 });
    twitterStreamHandler.send(new Object[] { "User1", "World", "IBM", 10 });
    SiddhiTestHelper.waitForEvents(100, 8, count, 6000);
    AssertJUnit.assertEquals(8, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 22 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class JoinPartitionTestCase method testJoinPartition10.

@Test
public void testJoinPartition10() throws InterruptedException {
    log.info("Join partition test10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "" + "define stream cseEventStream (symbol string, user string,volume int);  " + "define stream twitterStream (user string, tweet string, company string); " + "" + "partition with (user of cseEventStream, user of twitterStream) " + "begin " + "   @info(name = 'query1') " + "   from cseEventStream#window.length(1) unidirectional join twitterStream#window.length(1) " + "   select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.volume, " + "cseEventStream.user" + "   insert all events into outputStream1 ;" + "" + "end;" + "" + "partition with (user of outputStream1) " + "begin " + "   @info(name = 'query2') " + "   from outputStream1#window.length(1) join twitterStream#window.length(1) " + "   select outputStream1.symbol as symbol, twitterStream.tweet, outputStream1.volume " + "   insert all events into outputStream ;" + "" + "end;" + " ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
    siddhiAppRuntime.start();
    twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
    cseEventStreamHandler.send(new Object[] { "WSO2", "User1", 100 });
    cseEventStreamHandler.send(new Object[] { "WSO2", "User2", 100 });
    twitterStreamHandler.send(new Object[] { "User2", "Hello World", "WSO2" });
    twitterStreamHandler.send(new Object[] { "User3", "Hello World", "WSO2" });
    cseEventStreamHandler.send(new Object[] { "WSO2", "User3", 100 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
    AssertJUnit.assertEquals(3, 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 23 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery35.

@Test
public void testPartitionQuery35() throws InterruptedException {
    log.info("Partition test34");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest34') " + "define stream cseEventStream (atr1 string,  atr2 float, atr3 int, atr4 double, " + "atr5 long,  atr6 long,  atr7 double,  atr8 float , atr9 bool, atr10 bool,  atr11 int);" + "partition with (atr1 of cseEventStream) begin @info(name = 'query1') from " + "cseEventStream[700>atr5 OR atr8 > atr3 OR atr6 > atr2 OR atr4 " + "> atr6 OR atr2 > atr7 OR atr3 > atr4 OR atr4 > atr3 OR atr6 > atr3 OR atr3" + "> atr2 OR atr8 > atr5 OR atr6 > atr4 OR atr3 > atr6 OR atr7 > atr8 OR atr7 " + "> atr4 OR atr6 > atr5 OR atr11 > atr3 OR atr8 > atr2] select" + " atr1 as symbol, sum(atr2) 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);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 101.0, 500L, 200L, 102.0, 75.7f, false, true, 105 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100, 101.0, 501L, 201L, 103.0, 76.7f, false, true, 106 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 102.0, 502L, 202L, 104.0, 77.7f, false, true, 107 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100, 101.0, 502L, 202L, 104.0, 77.7f, false, true, 108 });
    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 24 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp 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 25 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp 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)

Aggregations

SiddhiManager (org.wso2.siddhi.core.SiddhiManager)273 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)272 Test (org.testng.annotations.Test)261 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)245 Event (org.wso2.siddhi.core.event.Event)241 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)138 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)109 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)88 Query (org.wso2.siddhi.query.api.execution.query.Query)84 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)81 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)13 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)13 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 HashMap (java.util.HashMap)6 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)6 InMemoryConfigManager (org.wso2.siddhi.core.util.config.InMemoryConfigManager)5 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4