Search in sources :

Example 26 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class JoinTestCase method joinTest20.

@Test
public void joinTest20() throws InterruptedException {
    log.info("Join test20");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream dataIn (id int, data string); " + "define stream countIn (id int); " + "define stream deleteIn (id int); " + "define table dataTable (id int, data string); ";
    String query = "" + "from dataIn\n" + "insert into dataTable;\n" + "" + "from deleteIn \n" + "delete dataTable\n" + "    on dataTable.id == id;\n" + "" + "@info(name = 'query1') " + "from countIn as c left outer join dataTable as d\n" + "on d.data == 'abc'\n" + "select count() as count\n" + "insert into countOut; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler dataIn = siddhiAppRuntime.getInputHandler("dataIn");
        InputHandler countIn = siddhiAppRuntime.getInputHandler("countIn");
        InputHandler deleteIn = siddhiAppRuntime.getInputHandler("deleteIn");
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                inEventCount.incrementAndGet();
                if (inEventCount.get() == 1) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(0) == 3L);
                } else {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    AssertJUnit.assertTrue((Long) inEvents[0].getData(0) == 2L);
                }
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(100);
        dataIn.send(new Object[] { 1, "abc" });
        dataIn.send(new Object[] { 2, "abc" });
        dataIn.send(new Object[] { 3, "abc" });
        countIn.send(new Object[] { 1 });
        deleteIn.send(new Object[] { 1 });
        countIn.send(new Object[] { 1 });
    } finally {
        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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 27 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class JoinTestCase method joinTest16.

@Test
public void joinTest16() throws InterruptedException {
    log.info("Join test16");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream order (billnum string, custid string, items string, dow string, timestamp long); " + "define table dow_items (custid string, dow string, item string) ; " + "define stream dow_items_stream (custid string, dow string, item string); ";
    String query = "" + "@info(name = 'query1') " + "from order join dow_items \n" + "on order.custid == dow_items.custid \n" + "select  order.custid\n" + "having dow_items.item == \"item1\" \n" + "insert into recommendationStream ;" + "@info(name = 'query2') " + "from dow_items_stream " + "insert into dow_items ;" + "" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler orderStream = siddhiAppRuntime.getInputHandler("order");
        InputHandler itemsStream = siddhiAppRuntime.getInputHandler("dow_items_stream");
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                eventArrived = true;
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(100);
        itemsStream.send(new Object[] { "cust1", "bill1", "item1" });
        orderStream.send(new Object[] { "bill1", "cust1", "item1", "dow1", 12323232L });
        Thread.sleep(100);
        AssertJUnit.assertEquals("Event Arrived", true, eventArrived);
    } finally {
        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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 28 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class SimpleQueryValidatorTestCase method testQueryWithEveryTable.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testQueryWithEveryTable() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String tables = "define table TestTable(symbol string, volume float);\n" + "" + "from every TestTable " + "select * " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(tables);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 29 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class Window method init.

/**
 * Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
 *
 * @param tableMap       map of {@link Table}s
 * @param eventWindowMap map of EventWindows
 * @param queryName      name of the query window belongs to.
 */
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String queryName) {
    if (this.windowProcessor != null) {
        return;
    }
    // Create and initialize MetaStreamEvent
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addInputDefinition(windowDefinition);
    metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
    metaStreamEvent.initializeAfterWindowData();
    for (Attribute attribute : windowDefinition.getAttributeList()) {
        metaStreamEvent.addOutputData(attribute);
    }
    this.streamEventPool = new StreamEventPool(metaStreamEvent, 5);
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventPool);
    OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
    boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;
    WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor(windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(), this.siddhiAppContext, tableMap, false, outputExpectsExpiredEvents, queryName);
    internalWindowProcessor.setStreamEventCloner(streamEventCloner);
    internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
    EntryValveProcessor entryValveProcessor = null;
    if (internalWindowProcessor instanceof SchedulingProcessor) {
        entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
        Scheduler scheduler = SchedulerParser.parse(this.siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, this.siddhiAppContext);
        scheduler.init(this.lockWrapper, queryName);
        scheduler.setStreamEventPool(streamEventPool);
        ((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
    }
    if (entryValveProcessor != null) {
        entryValveProcessor.setToLast(internalWindowProcessor);
        this.windowProcessor = entryValveProcessor;
    } else {
        this.windowProcessor = internalWindowProcessor;
    }
    // StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
    this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
    this.internalWindowProcessor = internalWindowProcessor;
}
Also used : Attribute(org.wso2.siddhi.query.api.definition.Attribute) Scheduler(org.wso2.siddhi.core.util.Scheduler) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) StreamEventCloner(org.wso2.siddhi.core.event.stream.StreamEventCloner) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 30 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class DefineTableTestCase method testQuery10.

@Test(expectedExceptions = DuplicateDefinitionException.class)
public void testQuery10() throws InterruptedException {
    log.info("testTableDefinition10 - OUT 0");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "define stream StockStream(symbol string, price int, volume float); " + "define table OutputStream (symbol string, price float, volume long);" + "" + "from StockStream " + "select symbol, price " + "insert into OutputStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)183 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)176 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)176 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)140 Event (org.wso2.siddhi.core.event.Event)119 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)102 ArrayList (java.util.ArrayList)47 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)23 HashMap (java.util.HashMap)22 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)20 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)19 Attribute (org.wso2.siddhi.query.api.definition.Attribute)19 Table (org.wso2.siddhi.core.table.Table)11 Expression (org.wso2.siddhi.query.api.expression.Expression)11 Variable (org.wso2.siddhi.query.api.expression.Variable)11 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)10 TableDefinition (org.wso2.siddhi.query.api.definition.TableDefinition)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)8