Search in sources :

Example 11 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery5.

@Test
public void testPartitionQuery5() throws InterruptedException {
    log.info("Partition test5");
    final SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan5");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition streamDefinition1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition streamDefinition2 = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    siddhiApp.defineStream(streamDefinition1);
    siddhiApp.defineStream(streamDefinition2);
    Partition partition = Partition.partition().with("cseEventStream", Expression.variable("symbol"));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream")).annotation(Annotation.annotation("info").element("name", "query"));
    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")).annotation(Annotation.annotation("info").element("name", "query1"));
    query1.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query1.insertInto("OutStockStream");
    // //////////////////////////////////partition-2/////////////////
    Partition partition1 = Partition.partition().with("cseEventStream1", Expression.variable("symbol"));
    Query query2 = Query.query();
    query2.from(InputStream.stream("cseEventStream1")).annotation(Annotation.annotation("info").element("name", "query2"));
    query2.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query2.insertIntoInner("StockStream");
    Query query3 = Query.query();
    query3.from(InputStream.innerStream("StockStream")).annotation(Annotation.annotation("info").element("name", "query3"));
    query3.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query3.insertInto("OutStockStream");
    Query query4 = Query.query();
    query4.from(InputStream.stream("StockStream")).annotation(Annotation.annotation("info").element("name", "query4"));
    query4.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query4.insertInto("OutStockStream");
    Query query5 = Query.query();
    query5.from(InputStream.innerStream("StockStream")).annotation(Annotation.annotation("info").element("name", "query5"));
    query5.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query5.insertInto("StockStream");
    partition.addQuery(query);
    partition.addQuery(query1);
    partition.addQuery(query5);
    partition1.addQuery(query2);
    partition1.addQuery(query3);
    siddhiApp.addPartition(partition);
    siddhiApp.addPartition(partition1);
    siddhiApp.addQuery(query4);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            stockStreamEventCount = stockStreamEventCount + events.length;
        }
    });
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            synchronized (siddhiManager) {
                count.addAndGet(events.length);
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler inputHandler2 = siddhiAppRuntime.getInputHandler("cseEventStream1");
    InputHandler inputHandler3 = siddhiAppRuntime.getInputHandler("StockStream");
    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 });
    inputHandler2.send(new Object[] { "IBM1", 75.6f, 100 });
    inputHandler2.send(new Object[] { "WSO21", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM1", 75.6f, 100 });
    inputHandler2.send(new Object[] { "ORACLE1", 75.6f, 100 });
    inputHandler3.send(new Object[] { "ABC", 75.6f, 100 });
    inputHandler3.send(new Object[] { "DEF", 75.6f, 100 });
    inputHandler3.send(new Object[] { "KLM", 75.6f, 100 });
    inputHandler3.send(new Object[] { "ABC", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 16, count, 60000);
    AssertJUnit.assertEquals(16, count.get());
    Thread.sleep(100);
    AssertJUnit.assertEquals(8, stockStreamEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery42.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testPartitionQuery42() throws InterruptedException {
    log.info("Partition test");
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp("Test").defineStream(StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT));
    Query query = Query.query();
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.from(InputStream.stream("streamA"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    Partition partition = Partition.partition();
    partition.addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    Thread.sleep(1000);
    AssertJUnit.assertEquals(0, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 13 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery44.

@Test
public void testPartitionQuery44() throws InterruptedException {
    log.info("Partition test44");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan44");
    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("e1", "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("e2", "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(io.siddhi.query.api.execution.partition.Partition) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 14 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery39.

@Test
public void testPartitionQuery39() throws InterruptedException {
    log.info("Partition test");
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp("Test").defineStream(StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT));
    Query query = Query.query();
    query.from(InputStream.stream("streamA"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    Partition partition = Partition.partition().annotation(Annotation.annotation("info").element("name", "partitionA")).with("streamA", Expression.variable("symbol")).addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    Thread.sleep(1000);
    AssertJUnit.assertEquals(3, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 15 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitPartition.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Partition visitPartition(@NotNull SiddhiQLParser.PartitionContext ctx) {
    Partition partition = Partition.partition();
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        partition.annotation((Annotation) visit(annotationContext));
    }
    for (SiddhiQLParser.Partition_with_streamContext with_streamContext : ctx.partition_with_stream()) {
        partition.with((PartitionType) visit(with_streamContext));
    }
    for (SiddhiQLParser.QueryContext queryContext : ctx.query()) {
        partition.addQuery((Query) visit(queryContext));
    }
    populateQueryContext(partition, ctx);
    return partition;
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiQLParser(io.siddhi.query.compiler.SiddhiQLParser)

Aggregations

Partition (io.siddhi.query.api.execution.partition.Partition)19 Query (io.siddhi.query.api.execution.query.Query)14 Test (org.testng.annotations.Test)13 SiddhiApp (io.siddhi.query.api.SiddhiApp)10 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)8 SiddhiManager (io.siddhi.core.SiddhiManager)8 Event (io.siddhi.core.event.Event)8 InputHandler (io.siddhi.core.stream.input.InputHandler)8 StreamCallback (io.siddhi.core.stream.output.StreamCallback)8 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)7 ExecutionElement (io.siddhi.query.api.execution.ExecutionElement)3 SiddhiQLParser (io.siddhi.query.compiler.SiddhiQLParser)2 ArrayList (java.util.ArrayList)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 PartitionRuntimeImpl (io.siddhi.core.partition.PartitionRuntimeImpl)1 QueryRuntimeImpl (io.siddhi.core.query.QueryRuntimeImpl)1 IdGenerator (io.siddhi.core.util.IdGenerator)1 SiddhiAppRuntimeBuilder (io.siddhi.core.util.SiddhiAppRuntimeBuilder)1