Search in sources :

Example 61 with Partition

use of org.wso2.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(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser)

Example 62 with Partition

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

the class DefinePartitionTestCase method test2.

@Test
public void test2() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (symbol of cseEventStream) begin from " + "cseEventStream select sum(volume) as sumvolume insert into StockStream ;  end ");
    AssertJUnit.assertEquals(Partition.partition().with("cseEventStream", Expression.variable("symbol")).toString().split("queryList")[0], partition.toString().split("queryList")[0]);
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) Test(org.testng.annotations.Test)

Example 63 with Partition

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

the class SiddhiApp method addPartition.

public SiddhiApp addPartition(Partition partition) {
    if (partition == null) {
        throw new SiddhiAppValidationException("Partition should not be null");
    }
    String name = null;
    Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_INFO, SiddhiConstants.ANNOTATION_ELEMENT_NAME, partition.getAnnotations());
    if (element != null) {
        name = element.getValue();
    }
    if (name != null && executionElementNameList.contains(name)) {
        throw new SiddhiAppValidationException("Cannot add Partition as another Execution Element already " + "uses its name=" + name, element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
    }
    executionElementNameList.add(name);
    this.executionElementList.add(partition);
    return this;
}
Also used : Element(org.wso2.siddhi.query.api.annotation.Element) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)

Example 64 with Partition

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

the class PartitionQueryTestCase method testCreatingFilterQuery.

// from StockStream[ 7+9.5>price AND 100>=volume]
// insert into OutStockStream symbol, avg(price) as avgPrice
// group by symbol
// having avgPrice>50
@Test
public void testCreatingFilterQuery() {
    Partition partition = Partition.partition().with("StockStream", Expression.variable("symbol")).with("StockStream1", Expression.variable("symbol")).with("StockStream2", Partition.range("LessValue", Expression.compare(Expression.value(7), Compare.Operator.GREATER_THAN, Expression.variable("price"))), Partition.range("HighValue", Expression.compare(Expression.value(9.5), Compare.Operator.LESS_THAN, Expression.variable("price1"))));
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertIntoInner("OutStockStream");
    partition.addQuery(query);
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 65 with Partition

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

the class PartitionSample method main.

public static void main(String[] args) throws InterruptedException {
    // Creating Siddhi Manager
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream cseEventStream (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 ";
    // Generating runtime
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    // Adding callback to retrieve output events from stream
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
        }
    });
    // Retrieving InputHandler to push events into Siddhi
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    // Starting event processing
    siddhiAppRuntime.start();
    // Sending events to Siddhi
    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 });
    Thread.sleep(1000);
    // Shutting down the runtime
    siddhiAppRuntime.shutdown();
    // Shutting down Siddhi
    siddhiManager.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)

Aggregations

Test (org.testng.annotations.Test)132 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)131 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)131 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)130 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)127 Event (org.wso2.siddhi.core.event.Event)125 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)13 Query (org.wso2.siddhi.query.api.execution.query.Query)11 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)7 ArrayList (java.util.ArrayList)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)4 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)4 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)4 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)3 Element (org.wso2.siddhi.query.api.annotation.Element)3 List (java.util.List)2 TestUtil (org.wso2.siddhi.core.TestUtil)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 PartitionRuntime (org.wso2.siddhi.core.partition.PartitionRuntime)2