io.siddhi.core.event.Event Java Examples

The following examples show how to use io.siddhi.core.event.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonSinkMapper.java    From siddhi-map-json with Apache License 2.0 6 votes vote down vote up
@Override
public void mapAndSend(Event[] events, OptionHolder optionHolder,
                       Map<String, TemplateBuilder> payloadTemplateBuilderMap, SinkListener sinkListener) {

    StringBuilder sb = new StringBuilder();
    if (payloadTemplateBuilderMap == null) {
        String jsonString = constructJsonForDefaultMapping(events);
        sb.append(jsonString);
    } else {
        sb.append(constructJsonForCustomMapping(events,
                payloadTemplateBuilderMap.get(payloadTemplateBuilderMap.keySet().iterator().next())));
    }

    if (!isJsonValidationEnabled) {
        sinkListener.publish(sb.toString());
    } else if (isValidJson(sb.toString())) {
        sinkListener.publish(sb.toString());
    } else {
        log.error("Invalid json string : " + sb.toString() + ". Hence dropping the message.");
    }
}
 
Example #2
Source File: StreamJunction.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private void sendEvent(Event[] events) {
    if (throughputTracker != null && Level.BASIC.compareTo(siddhiAppContext.getRootMetricsLevel()) <= 0) {
        throughputTracker.eventsIn(events.length);
    }
    if (isTraceEnabled) {
        log.trace("Event is received by streamJunction " + this);
    }
    if (disruptor != null) {
        for (Event event : events) {   // Todo : optimize for arrays
            long sequenceNo = ringBuffer.next();
            try {
                EventExchangeHolder eventExchangeHolder = ringBuffer.get(sequenceNo);
                eventExchangeHolder.getEvent().copyFrom(event);
                eventExchangeHolder.getAndSetIsProcessed(false);
            } finally {
                ringBuffer.publish(sequenceNo);
            }
        }
    } else {
        for (Receiver receiver : receivers) {
            receiver.receive(events);
        }
    }
}
 
Example #3
Source File: HdfsUserCommandReassembler.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
    Object[] attrValues = inEvents[0].getData();
    OutputCollector collector = (OutputCollector) attrValues[0];
    SortedMap<String, Object> outputEvent = new TreeMap<String, Object>();
    int i = 1;  // output is from second element
    String user = null;
    for(String attrKey : outputSelector.keySet()){
        Object v = attrValues[i++];
        outputEvent.put(attrKey, v);
        if(attrKey.equals("user"))
            user = (String)v;
    }

    outputEvent.putAll(outputModifier);
    LOG.debug("outputEvent: " + outputEvent);
    collector.emit(Arrays.asList(user, outputEvent));
}
 
Example #4
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery11() throws InterruptedException {
    log.info("Filter test11");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume double);";
    String query = "@info(name = 'query1') from cseEventStream[volume > 50f] select symbol,price insert into " +
            "outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #5
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery72() throws InterruptedException {
    log.info("Filter test72");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE)
            .attribute("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_THAN_EQUAL,
            Expression.value(200L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 500f, 60d, 5});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example #6
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery83() throws InterruptedException {
    log.info("Filter test83");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE)
            .attribute("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN,
            Expression.value(60L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 500f, 50d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #7
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery10() throws InterruptedException {
    log.info("Filter test10");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume double);";
    String query = "@info(name = 'query1') from cseEventStream[volume > 50d] select symbol,price insert into " +
            "outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #8
Source File: UpdateTestStoreTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void updateFromTableTest9() throws InterruptedException, SQLException {
    log.info("updateFromTableTest9");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price double, volume long); " +
            "define stream UpdateStockStream (symbol string, price double, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price double, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from UpdateStockStream " +
            "select symbol, price, volume " +
            "update StockTable " +
            "   on (StockTable.symbol == symbol and StockTable.volume > volume) ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6, 50L});
    stockStream.send(new Object[]{"IBM", 75.6, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6, 200L});
    updateStockStream.send(new Object[]{"WSO2", 85.6, 100L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);

    siddhiAppRuntime.shutdown();
}
 
Example #9
Source File: UpdateOrInsertTableWithCacheTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void updateOrInsertTableTest12() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream UpdateStockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from UpdateStockStream " +
            "update or insert into StockTable " +
            "   on StockTable.volume == volume ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6F, 100L});
    stockStream.send(new Object[]{"IBM", 75.6F, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6F, 100L});
    updateStockStream.send(new Object[]{"GOOG", 10.6F, 200L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(4, events.length);

    siddhiAppRuntime.shutdown();
}
 
Example #10
Source File: InputEventHandler.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public void sendEvent(Event event) throws InterruptedException {
    try {
        if (latencyTracker != null && Level.BASIC.compareTo(siddhiAppContext.getRootMetricsLevel()) <= 0) {
            latencyTracker.markOut();
        }
        Object[] transportProperties = trpProperties.get();
        trpProperties.remove();
        String[] transportSyncProperties = trpSyncProperties.get();
        trpSyncProperties.remove();
        if (event.getTimestamp() == -1) {
            long currentTimestamp = timestampGenerator.currentTime();
            event.setTimestamp(currentTimestamp);
        }
        for (int i = 0; i < transportMapping.size(); i++) {
            AttributeMapping attributeMapping = transportMapping.get(i);
            event.getData()[attributeMapping.getPosition()] = AttributeConverter.getPropertyValue(
                    transportProperties[i], attributeMapping.getType());
        }
        inputEventHandlerCallback.sendEvent(event, transportSyncProperties);
        eventCount++;
    } catch (RuntimeException e) {
        LOG.error(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) +
                " Error in applying transport property mapping for '" + sourceType
                + "' source at '" + inputHandler.getStreamId() + "' stream.", e);
    } finally {
        trpProperties.remove();
        trpSyncProperties.remove();
    }
}
 
Example #11
Source File: OnDemandQueryTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test17() throws InterruptedException {
    log.info("Testing delete on-demand query : 2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 200L});
    stockStream.send(new Object[]{"GOOGLE", 57.6f, 300L});
    Thread.sleep(500);


    Event[] initialEvents = siddhiAppRuntime.query("from StockTable select *;");
    Assert.assertEquals(initialEvents.length, 3);

    siddhiAppRuntime.query("delete StockTable on StockTable.volume == 100L;");
    Thread.sleep(500);

    Event[] allEvents = siddhiAppRuntime.query("from StockTable select *;");
    Assert.assertEquals(allEvents.length, 2);

    Event[] events = siddhiAppRuntime.query("from StockTable select * having volume == 100L");
    Assert.assertNull(events);

    siddhiAppRuntime.shutdown();
}
 
Example #12
Source File: AndAggregatorExtensionTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void andAggregatorTest7() throws InterruptedException {

    log.info("andAggregator Test 7");

    SiddhiManager siddhiManager = new SiddhiManager();

    String execPlan = "" +
            "@app:name('andAggregatorTests') " +
            "" +
            "define stream cseEventStream (name string, isFraud bool);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select and(isFraud) as isFraudTransaction " +
            "insert into outputStream;";

    SiddhiAppRuntime execPlanRunTime = siddhiManager.createSiddhiAppRuntime(execPlan);
    execPlanRunTime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {

            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertEquals(true, inEvents[0].getData()[0]);
        }
    });

    InputHandler inputHandler = execPlanRunTime.getInputHandler("cseEventStream");

    execPlanRunTime.start();
    inputHandler.send(new Object[]{"Nayana", "True"});
    Thread.sleep(100);
    execPlanRunTime.shutdown();
}
 
Example #13
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery37() throws InterruptedException {
    log.info("Filter test37");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume double);";
    String query = "@info(name = 'query1') from cseEventStream[volume == 50d] select symbol,price,volume insert " +
            "into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"WSO2", 55.5f, 40d});
    inputHandler.send(new Object[]{"WSO2", 53.5f, 50d});

    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example #14
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest20"}, expectedExceptions =
        StoreQueryCreationException.class)
public void incrementalStreamProcessorTest21() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest21");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " +
            "as lastTradeValue  " +
            "group by symbol " +
            "aggregate by timestamp every sec...hour ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    siddhiAppRuntime.start();
    Thread.sleep(100);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2017-06-** **:**:**\" " +
            "per \"days\"");
    EventPrinter.print(events);

    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
 
Example #15
Source File: StringListSizeFunctionExtensionTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringListSize() throws Exception {
    Semaphore semp = new Semaphore(1);
    String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
            " from log select str:listSize(switchLabel) as alertKey insert into output; ";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events.length == 1);
            Assert.assertTrue(Integer.parseInt(events[0].getData(0).toString()) == 5);
            semp.release();
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");
    semp.acquire();
    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
    String rhs = "[\"b\", \"d\"]";
    e.setData(new Object[] {System.currentTimeMillis(), ths, "port01", rhs});
    logInput.send(e);

    semp.acquire();
    runtime.shutdown();

}
 
Example #16
Source File: QueryAPITestCaseForTestStore.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = StoreQueryCreationException.class)
public void test4() throws InterruptedException {
    log.info("Test4 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
        stockStream.send(new Object[]{"IBM", 75.6f, 100L});
        stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
        Thread.sleep(500);

        Event[] events = siddhiAppRuntime.query("" +
                "from StockTabled " +
                "on price > 5 " +
                "select symbol1, sum(volume) as totalVolume " +
                "group by symbol " +
                "having totalVolume >150 ");
        EventPrinter.print(events);
        AssertJUnit.assertEquals(1, events.length);
        AssertJUnit.assertEquals(400L, events[0].getData(1));

    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example #17
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery15() throws InterruptedException {
    log.info("Filter test15");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume float, quantity " +
            "int);";
    String query = "@info(name = 'query1') from cseEventStream[quantity > 4d] select symbol,price,quantity insert" +
            " into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"WSO2", 50f, 60d, 5});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 200d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example #18
Source File: TimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void timeWindowBatchTest2() throws InterruptedException {

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.timeBatch(1 sec) " +
            "select symbol, sum(price) as price " +
            "insert all events into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 1});
    Thread.sleep(1100);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 2});
    inputHandler.send(new Object[]{"IBM", 700f, 3});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 4});
    Thread.sleep(1100);
    inputHandler.send(new Object[]{"IBM", 700f, 5});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 6});
    Thread.sleep(2000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(1, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example #19
Source File: ExternalTimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testExternalTimeBatchWindow9() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test9");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream LoginEvents (timestamp long, ip string); " +
            "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 2 sec); ";
    String query = "" +
            "@info(name = 'query0') " +
            "from LoginEvents " +
            "insert into LoginWindow; " +
            "" +
            "@info(name = 'query1') " +
            "from LoginWindow " +
            "select timestamp, ip, count() as total  " +
            "group by ip " +
            "insert into uniqueIps ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("uniqueIps", new StreamCallback() {
        @Override
        public synchronized void receive(Event[] events) {
            if (events != null) {
                inEventCount = inEventCount + events.length;
                for (Event event : events) {
                    sum = sum + (Long) event.getData(2);
                }
            }
            eventArrived = true;
        }
    });

    final InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            int i = 0;
            long time = 1366335804341L;
            while (i < 10000) {

                try {
                    inputHandler.send(new Object[]{time, "192.10.1." + Thread.currentThread().getId()});
                } catch (InterruptedException e) {
                    log.error(e.getMessage(), e);
                }
                time += 1000;
                i++;
            }
        }
    };
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();
    new Thread(runnable).start();


    Thread.sleep(10000);
    assertEquals(10 * 10000, sum);
    siddhiAppRuntime.shutdown();
}
 
Example #20
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery8() throws InterruptedException {
    log.info("Partition test8");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "@app:name('PartitionTest8') " +
            "define stream cseEventStream (symbol string, price float,volume int);"
            + "define stream cseEventStream1 (symbol string, price float,volume int);"
            + "partition with (symbol of cseEventStream)"
            + "begin"
            + "@info(name = 'query') from cseEventStream select symbol,max(price) as max_price,volume insert into" +
            " OutStockStream ;"
            + "end ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(75.0f, event.getData()[1]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(705.0f, event.getData()[1]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(75.0f, event.getData()[1]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(50.0f, event.getData()[1]);
                }
                eventArrived = true;
            }
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    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});
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #21
Source File: LengthBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void lengthBatchWindowTest13() throws InterruptedException {

    log.info("LengthBatchWindow Test13");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from cseEventStream#window.lengthBatch(2,true) join twitterStream#window.lengthBatch(2,true) " +
            "on cseEventStream.symbol== twitterStream.company " +
            "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " +
            "insert all events into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {
            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {

                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[]{"WSO2", 55.6f, 100});
        twitterStreamHandler.send(new Object[]{"User1", "Hello World", "WSO2"});
        cseEventStreamHandler.send(new Object[]{"IBM", 75.6f, 100});
        cseEventStreamHandler.send(new Object[]{"WSO2", 57.6f, 100});
        AssertJUnit.assertEquals(2, inEventCount);
        AssertJUnit.assertEquals(1, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example #22
Source File: JoinTableTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTableJoinQuery3() throws InterruptedException {
    log.info("testTableJoinQuery3 - OUT 1");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string); " +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from CheckStockStream#window.length(1) join StockTable as t " +
            " on CheckStockStream.symbol!=t.symbol " +
            "select CheckStockStream.symbol as checkSymbol, t.symbol as symbol, t.volume as volume  " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    siddhiAppRuntime.addCallback("query2", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch (inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[]{"WSO2", "IBM", 10L}, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 10L});
    checkStockStream.send(new Object[]{"WSO2"});

    Thread.sleep(500);

    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

    siddhiAppRuntime.shutdown();

}
 
Example #23
Source File: InputDistributor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
public void send(List<Event> events, int streamIndex) {
    inputProcessors.get(streamIndex).send(events, streamIndex);
}
 
Example #24
Source File: PatternPartitionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPatternPartitionQuery7() throws InterruptedException {
    log.info("Partition - testPatternEvery7 - OUT 2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); ";
    String partitionStart = "partition with (volume of Stream1,volume of Stream2) begin ";

    String query = "" +
            "@info(name = 'query1') " +
            "from  every ( e1=Stream1[price>20] -> e3=Stream1[price>20]) " +
            "select e1.price as price1, e3.price as price3 " +
            "insert into OutputStream ;";

    String partitionEnd = "end";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart
            + query + partitionEnd);

    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    switch (inEventCount.get()) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[]{55.6f, 57.6f}, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[]{54f, 53.6f}, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount.get());
                    }
                }
                eventArrived = true;
            }
        }
    });


    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");

    siddhiAppRuntime.start();

    stream1.send(new Object[]{"MSFT", 55.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"WSO2", 57.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"GOOG", 54f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"WSO2", 53.6f, 100});
    SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

    siddhiAppRuntime.shutdown();
}
 
Example #25
Source File: KafkaSourceTestCase.java    From siddhi-io-kafka with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "testKafkaWithoutTopicSource")
public void testKafkaMultipleTopicSource() throws InterruptedException {
    try {
        log.info("-------------------------------------------------------------------------------------------");
        log.info("Creating test for multiple topic");
        log.info("-------------------------------------------------------------------------------------------");
        String topics[] = new String[]{"multiple_topic1", "multiple_topic2"};
        receivedEventNameList = new ArrayList<>(4);
        receivedValueList = new ArrayList<>(4);
        KafkaTestUtil.createTopic(topics, 1);
        SiddhiManager siddhiManager = new SiddhiManager();
        SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(
                "@App:name('TestExecutionPlan') " +
                        "define stream BarStream (symbol string, price float, volume long); " +
                        "@info(name = 'query1') " +
                        "@source(type='kafka', topic.list='multiple_topic1,multiple_topic2', "
                        + "group.id='test_multiple_topic1_multiple_topic2', " +
                        "threading.option='single.thread', bootstrap.servers='localhost:9092'," +
                        "@map(type='xml'))" +
                        "Define stream FooStream (symbol string, price float, volume long);" +
                        "from FooStream select symbol, price, volume insert into BarStream;");
        siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {
            @Override
            public void receive(Event[] events) {
                for (Event event : events) {
                    log.info(event);
                    eventArrived = true;
                    count++;
                    receivedEventNameList.add(event.getData(0).toString());
                    receivedValueList.add((long) event.getData(2));
                }
            }
        });
        siddhiAppRuntime.start();
        KafkaTestUtil.kafkaPublisher(topics, 1, 2, false, null, true);
        Thread.sleep(100);
        List<String> expectedNames = new ArrayList<>(2);
        expectedNames.add("multiple_topic1");
        expectedNames.add("multiple_topic1");
        expectedNames.add("multiple_topic2");
        expectedNames.add("multiple_topic2");
        List<Long> expectedValues = new ArrayList<>(2);
        expectedValues.add(0L);
        expectedValues.add(1L);
        expectedValues.add(0L);
        expectedValues.add(1L);
        AssertJUnit.assertEquals(4, count);
        AssertJUnit.assertEquals("Kafka Source expected input not received", expectedNames,
                receivedEventNameList);
        AssertJUnit.assertEquals("Kafka Source expected input not received", expectedValues, receivedValueList);
        KafkaTestUtil.deleteTopic(topics);
        siddhiAppRuntime.shutdown();
    } catch (ZkTimeoutException ex) {
        log.warn("No zookeeper may not be available.", ex);
    }
}
 
Example #26
Source File: JoinTableWithCacheTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTableJoinQuery9() throws InterruptedException, SQLException {
    log.info("testTableJoinQuery9 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream CheckStockStream (symbol string); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "@connection(maxWait = '4000')" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from CheckStockStream#window.length(1) join StockTable " +
            "select CheckStockStream.symbol as checkSymbol, StockTable.symbol as symbol, " +
            "StockTable.volume as volume  " +
            "insert into OutputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch (inEventCount) {
                        case 1:
                            Assert.assertEquals(event.getData(), new Object[]{"WSO2", "WSO2", 100L});
                            break;
                        case 2:
                            Assert.assertEquals(event.getData(), new Object[]{"WSO2", "IBM", 10L});
                            break;
                        default:
                            Assert.assertSame(inEventCount, 2);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }

    });

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 10L});
    checkStockStream.send(new Object[]{"WSO2"});
    Thread.sleep(1000);

    Assert.assertEquals(inEventCount, 2, "Number of success events");
    Assert.assertEquals(removeEventCount, 0, "Number of remove events");
    Assert.assertEquals(eventArrived, true, "Event arrived");
    siddhiAppRuntime.shutdown();
}
 
Example #27
Source File: ExtensionSample.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        // Creating Siddhi Manager
        SiddhiManager siddhiManager = new SiddhiManager();

        //Register the extension to Siddhi Manager
        siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);

        //Siddhi Application
        String siddhiApp = "" +
                "define stream StockStream (symbol string, price long, volume long);" +
                "" +
                "@info(name = 'query1') " +
                "from StockStream " +
                "select symbol , custom:plus(price, volume) as totalCount " +
                "insert into Output;";

        //Generating runtime
        SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

        //Adding callback to retrieve output events from query
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {
            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
            }
        });

        //Retrieving InputHandler to push events into Siddhi
        InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");

        //Starting event processing
        siddhiAppRuntime.start();

        //Sending events to Siddhi
        inputHandler.send(new Object[]{"IBM", 700L, 100L});
        inputHandler.send(new Object[]{"WSO2", 600L, 200L});
        inputHandler.send(new Object[]{"GOOG", 60L, 200L});
        Thread.sleep(500);

        //Shutting down the runtime
        siddhiAppRuntime.shutdown();

        //Shutting down Siddhi
        siddhiManager.shutdown();

    }
 
Example #28
Source File: KafkaSinkTestCase.java    From siddhi-io-kafka with Apache License 2.0 4 votes vote down vote up
public void testPublisherWithKafkaTransportWithDynamicTopicAndPartition() throws InterruptedException {
    LOG.info("Creating test for publishing events for dynamic topic with dynamic partition");
    try {
        String topics[] = new String[]{"multiple_topic1_two_par_all_sub1", "multiple_topic2_two_par_all_sub1"};
        receivedEventNameList = new ArrayList<>(4);
        receivedValueList = new ArrayList<>(4);
        KafkaTestUtil.createTopic(topics, 2);
        Thread.sleep(10000);
        SiddhiManager siddhiManager = new SiddhiManager();
        SiddhiAppRuntime executionPlanRuntime = siddhiManager.createSiddhiAppRuntime(
                "@App:name('TestExecutionPlan14') " +
                        "define stream FooStream (symbol string, price float, volume long); " +
                        "@info(name = 'query1') " +
                        "@sink(type='kafka', topic='{{symbol}}', partition.no='{{volume}}', "
                        + "bootstrap.servers='localhost:9092', " +
                        "@map(type='xml'))" +
                        "Define stream BarStream (symbol string, price float, volume long);" +
                        "from FooStream select symbol, price, volume insert into BarStream; ");
        InputHandler fooStream = executionPlanRuntime.getInputHandler("FooStream");
        executionPlanRuntime.start();


        SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(
                "@App:name('TestExecutionPlan15') " +
                        "define stream BarStream (symbol string, price float, volume long); " +
                        "@info(name = 'query1') " +
                        "@source(type='kafka', "
                        + "topic.list='multiple_topic1_two_par_all_sub1,multiple_topic2_two_par_all_sub1', "
                        + "group.id='multiple_topic1_two_par_all_sub1_test', threading.option='single.thread', "
                        + "bootstrap.servers='localhost:9092', "
                        + "partition.no.list='0,1'," +
                        "@map(type='xml'))" +
                        "Define stream FooStream (symbol string, price float, volume long);" +
                        "from FooStream select symbol, price, volume insert into BarStream;");
        siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {
            @Override
            public void receive(Event[] events) {
                for (Event event : events) {
                    LOG.info(event);
                    eventArrived = true;
                    count++;
                    receivedEventNameList.add(event.getData(0).toString());
                    receivedValueList.add((long) event.getData(2));
                }
            }
        });
        siddhiAppRuntime.start();
        Thread.sleep(2000);
        fooStream.send(new Object[]{"multiple_topic1_two_par_all_sub1", 55.6f, 0L});
        Thread.sleep(1000);
        fooStream.send(new Object[]{"multiple_topic1_two_par_all_sub1", 55.6f, 1L});
        Thread.sleep(1000);
        fooStream.send(new Object[]{"multiple_topic2_two_par_all_sub1", 75.6f, 0L});
        Thread.sleep(1000);
        fooStream.send(new Object[]{"multiple_topic2_two_par_all_sub1", 75.6f, 1L});
        Thread.sleep(5000);

        List<String> expectedNames = new ArrayList<>(2);
        expectedNames.add("multiple_topic1_two_par_all_sub1");
        expectedNames.add("multiple_topic1_two_par_all_sub1");
        expectedNames.add("multiple_topic2_two_par_all_sub1");
        expectedNames.add("multiple_topic2_two_par_all_sub1");
        List<Long> expectedValues = new ArrayList<>(2);
        expectedValues.add(0L);
        expectedValues.add(1L);
        expectedValues.add(0L);
        expectedValues.add(1L);
        AssertJUnit.assertEquals(4, count);
        AssertJUnit.assertEquals("Kafka Source expected input not received", expectedNames,
                receivedEventNameList);
        AssertJUnit.assertEquals("Kafka Source expected input not received", expectedValues, receivedValueList);
        KafkaTestUtil.deleteTopic(topics);
        executionPlanRuntime.shutdown();
    } catch (ZkTimeoutException ex) {
        LOG.warn("No zookeeper may not be available.", ex);
    }
}
 
Example #29
Source File: TimeBatchWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTimeWindowBatch3() throws InterruptedException {
    log.info("TimeWindowBatch Test3");

    SiddhiManager siddhiManager = new SiddhiManager();

    String cseEventStream = "" +
            "define stream cseEventStream (symbol string, price float, volume int); " +
            "define window cseEventWindow (symbol string, price float, volume int) timeBatch(1 sec) output " +
            "current events; ";

    String query = "" +
            "@info(name = 'query0') " +
            "from cseEventStream " +
            "insert into cseEventWindow; " +
            "" +
            "@info(name = 'query1') " +
            "from cseEventWindow " +
            "select symbol, sum(price) as price " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            AssertJUnit.assertTrue("Remove events should not arrive ", removeEvents == null);
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 1});
    Thread.sleep(1100);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 2});
    inputHandler.send(new Object[]{"IBM", 700f, 3});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 4});
    Thread.sleep(1100);
    inputHandler.send(new Object[]{"IBM", 700f, 5});
    inputHandler.send(new Object[]{"WSO2", 60.5f, 6});
    Thread.sleep(2000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(0, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example #30
Source File: Aggregation2TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest5SGTTimeZoneDay2"})
public void incrementalStreamProcessorTest5SGTTimeZoneDay3() throws InterruptedException {
    //feb 28 non-leap
    LOG.info("incrementalStreamProcessorTest5");
    SiddhiManager siddhiManager = new SiddhiManager();
    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("aggTimeZone", "Asia/Singapore");
    ConfigManager configManager = new InMemoryConfigManager(null, null, systemConfigs);
    siddhiManager.setConfigManager(configManager);

    String stockStream =
            "define stream stockStream (symbol string, price float, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice  " +
            "group by symbol " +
            "aggregate by timestamp every day ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    // Saturday February 28, 2019 00:00:01 (am) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 1551283201000L});

    // Saturday February 28, 2019 23:59:59 (pm) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, 1551369599000L});

    // Sunday March 01, 2019 00:00:01 (am) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 80f, 1551369601000L});

    Thread.sleep(2000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2019-**-** **:**:**\" " +
            "per \"day\"");
    EventPrinter.print(events);

    Assert.assertNotNull(events, "Queried results cannot be null.");
    AssertJUnit.assertEquals(2, events.length);

    List<Object[]> eventsOutputList = new ArrayList<>();
    for (Event event : events) {
        eventsOutputList.add(event.getData());
    }
    List<Object[]> expected = Arrays.asList(
            new Object[]{1551283200000L, "WSO2", 60.0},
            new Object[]{1551369600000L, "WSO2", 80.0}
    );
    AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsOutputList, expected));
    siddhiAppRuntime.shutdown();
}