use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class LatestAggregationTestCase method latestTestCase2.
@Test(dependsOnMethods = "latestTestCase1")
public void latestTestCase2() throws InterruptedException {
LOG.info("latestTestCase2: testing latest incremental aggregator - different group by");
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, (price * quantity) as latestPrice " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select s.symbol, s.latestPrice " + "group by s.symbol " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
if (removeEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : removeEvents) {
removeEventsList.add(event.getData());
removeEventCount.incrementAndGet();
}
}
eventArrived = true;
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:50 AM - Out of order event
stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { "WSO22", 750f }, new Object[] { "WSO24", 1600f }, new Object[] { "IBM1", 3500f });
SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 10000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Number of remove events", 3, removeEventCount.get());
AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isUnsortedEventsMatch(removeEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase6.
@Test(dependsOnMethods = "aggregationFunctionTestcase5")
public void aggregationFunctionTestcase6() throws InterruptedException {
LOG.info("aggregationFunctionTestcase2 - count with different group by");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, name string, price float, lastClosingPrice float, " + "volume long , quantity int, timestamp long);";
String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, name, count() as count " + "group by symbol, name " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select s.symbol, sum(count) as count " + "group by s.symbol " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", "WSO21", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", "WSO22", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 100f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", "IBM2", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", "IBM1", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", "CISCO1", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", 4L }, new Object[] { "IBM", 6L }, new Object[] { "CISCO", 1L });
SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase15.
@Test(dependsOnMethods = { "aggregationFunctionTestcase13" })
public void aggregationFunctionTestcase15() throws InterruptedException {
LOG.info("aggregationFunctionTestcase15 - Group by AGG_TIMESTAMP");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "@purge(enable='false')" + " define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice " + "group by symbol " + "aggregate by timestamp every sec...hour ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
siddhiAppRuntime.start();
// 1 June 2017 04:05:50
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "IBM", 70f, null, 40L, 10, 1496289950000L });
// 1 June 2017 04:05:52
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289952500L });
// 1 June 2017 04:05:54
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289954500L });
Thread.sleep(2000);
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"2017-06-** **:**:**\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, sum(totalPrice) as totalPrice " + "group by AGG_TIMESTAMP;");
EventPrinter.print(events);
Assert.assertNotNull(events);
AssertJUnit.assertEquals(3, events.length);
List<Object[]> eventsOutputList = new ArrayList<>();
for (Event event : events) {
eventsOutputList.add(event.getData());
}
List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, 120.0 }, new Object[] { 1496289952000L, 160.0 }, new Object[] { 1496289954000L, 200.0 });
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsOutputList, expected));
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase4.
@Test(dependsOnMethods = "aggregationFunctionTestcase3")
public void aggregationFunctionTestcase4() throws InterruptedException {
LOG.info("aggregationFunctionTestcase2 - count with same group by");
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, count() as count " + "group by symbol " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select s.symbol, sum(count) as count " + "group by s.symbol " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", 4L }, new Object[] { "IBM", 6L }, new Object[] { "CISCO", 1L });
SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase3.
@Test(dependsOnMethods = "aggregationFunctionTestcase2")
public void aggregationFunctionTestcase3() throws InterruptedException {
LOG.info("aggregationFunctionTestcase2 - count w/o group by");
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, count() as count " + "group by symbol " + "aggregate by timestamp every sec, min ;" + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596434876000L " + "per \"seconds\" " + "select AGG_TIMESTAMP, s.symbol, s.count " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
Thread.sleep(2000);
inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "seconds" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 2L }, new Object[] { 1496289952000L, "WSO2", 2L }, new Object[] { 1496289954000L, "IBM", 2L }, new Object[] { 1496289956000L, "IBM", 2L }, new Object[] { 1496290016000L, "IBM", 1L }, new Object[] { 1496290076000L, "IBM", 1L }, new Object[] { 1496293676000L, "CISCO", 1L });
SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
Aggregations