Search in sources :

Example 6 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery4.

@Test
public void testPartitionQuery4() throws InterruptedException {
    log.info("Partition test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan4");
    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);
    siddhiApp.defineStream(streamDefinition);
    siddhiApp.defineStream(streamDefinition1);
    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-2/////////////////
    Partition partition1 = Partition.partition().with("cseEventStream1", Expression.variable("symbol"));
    Query query2 = Query.query();
    query2.from(InputStream.stream("cseEventStream1"));
    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"));
    query3.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query3.insertInto("OutStockStream");
    partition.addQuery(query);
    partition.addQuery(query1);
    partition1.addQuery(query2);
    partition1.addQuery(query3);
    siddhiApp.addPartition(partition);
    siddhiApp.addPartition(partition1);
    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");
    InputHandler inputHandler2 = siddhiAppRuntime.getInputHandler("cseEventStream1");
    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[] { "IBM", 75.6f, 100 });
    inputHandler2.send(new Object[] { "WSO2", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler2.send(new Object[] { "ORACLE", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 8, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(8, count.get());
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 7 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 8 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

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(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 9 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

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(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 10 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitSiddhi_app.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public SiddhiApp visitSiddhi_app(@NotNull SiddhiQLParser.Siddhi_appContext ctx) {
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp();
    for (SiddhiQLParser.App_annotationContext annotationContext : ctx.app_annotation()) {
        siddhiApp.annotation((Annotation) visit(annotationContext));
    }
    for (SiddhiQLParser.Definition_streamContext streamContext : ctx.definition_stream()) {
        siddhiApp.defineStream((StreamDefinition) visit(streamContext));
    }
    for (SiddhiQLParser.Definition_tableContext tableContext : ctx.definition_table()) {
        siddhiApp.defineTable((TableDefinition) visit(tableContext));
    }
    for (SiddhiQLParser.Definition_functionContext functionContext : ctx.definition_function()) {
        siddhiApp.defineFunction((FunctionDefinition) visit(functionContext));
    }
    for (SiddhiQLParser.Definition_windowContext windowContext : ctx.definition_window()) {
        siddhiApp.defineWindow((WindowDefinition) visit(windowContext));
    }
    for (SiddhiQLParser.Definition_aggregationContext aggregationContext : ctx.definition_aggregation()) {
        siddhiApp.defineAggregation((AggregationDefinition) visit(aggregationContext));
    }
    for (SiddhiQLParser.Execution_elementContext executionElementContext : ctx.execution_element()) {
        ExecutionElement executionElement = (ExecutionElement) visit(executionElementContext);
        if (executionElement instanceof Partition) {
            siddhiApp.addPartition((Partition) executionElement);
        } else if (executionElement instanceof Query) {
            siddhiApp.addQuery((Query) executionElement);
        } else {
            throw newSiddhiParserException(ctx);
        }
    }
    for (SiddhiQLParser.Definition_triggerContext triggerContext : ctx.definition_trigger()) {
        siddhiApp.defineTrigger((TriggerDefinition) visit(triggerContext));
    }
    populateQueryContext(siddhiApp, ctx);
    return siddhiApp;
}
Also used : SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser) Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) ExecutionElement(org.ballerinalang.siddhi.query.api.execution.ExecutionElement)

Aggregations

Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)13 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)9 Test (org.testng.annotations.Test)9 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)7 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)6 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)6 Event (org.ballerinalang.siddhi.core.event.Event)6 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)6 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)6 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)4 ExecutionElement (org.ballerinalang.siddhi.query.api.execution.ExecutionElement)2 SiddhiQLParser (org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ArrayList (java.util.ArrayList)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 PartitionRuntime (org.ballerinalang.siddhi.core.partition.PartitionRuntime)1