use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest12.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest11" })
public void incrementalStreamProcessorTest12() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest12");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "" + "@BufferSize('3') " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, " + "(price * quantity) as lastTradeValue, max(price) as maxPrice, min(price) as minPrice " + "group by symbol " + "aggregate by timestamp every sec...year ; " + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "from inputStream as i join stockAggregation as s " + "within \"2017-06-01 04:05:50\", \"2017-06-01 04:06:57\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, totalPrice, avgPrice, lastTradeValue, s.symbol, maxPrice, minPrice " + "order by AGG_TIMESTAMP " + "insert into tempStream; " + "@info(name = 'query1') " + "from tempStream " + "select totalPrice, avgPrice, lastTradeValue, symbol, maxPrice, minPrice " + "insert 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[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:53 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289953000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289953000L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289952000L });
// Thursday, June 1, 2017 4:05:51 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289951000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289951000L });
// Thursday, June 1, 2017 4:05:53 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289953000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 140f, null, 200L, 11, 1496289953000L });
// 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:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, 1496289954000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 1000f, null, 200L, 9, 1496290016000L });
Thread.sleep(100);
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[] { 240.0, 60.0, 700f, "WSO2", 70f, 50f }, new Object[] { 200.0, 100.0, 9600f, "IBM", 100f, 100f }, new Object[] { 1400.0, 700.0, 3500f, "IBM", 900f, 500f }, new Object[] { 300.0, 100.0, 1540f, "WSO2", 140f, 60f }, new Object[] { 400.0, 400.0, 3600f, "IBM", 400f, 400f }, new Object[] { 600.0, 600.0, 3600f, "IBM", 600f, 600f }, new Object[] { 1000.0, 1000.0, 9000f, "IBM", 1000f, 1000f });
SiddhiTestHelper.waitForEvents(100, 7, inEventCount, 60000);
AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Number of success events", 7, inEventCount.get());
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest41.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest40" })
public void incrementalStreamProcessorTest41() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest41");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "@BufferSize('3') " + "define aggregation stockAggregation " + "from stockStream " + "select avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) as " + "lastTradeValue, " + "count() as count " + "aggregate by timestamp every sec...year ;" + "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 \"2017-06-01 04:05:**\" " + "per \"seconds\" " + "select AGG_TIMESTAMP, s.avgPrice, totalPrice, lastTradeValue, count " + "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[] { "WSO2", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:49 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, 1496289949000L });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, 1496289949000L });
// Thursday, June 1, 2017 4:05:48 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289948000L });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, 1496289948000L });
// Thursday, June 1, 2017 4:05:47 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289947000L });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, 1496289947000L });
// Thursday, June 1, 2017 4:05:46 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289946000L });
Thread.sleep(100);
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[] { 1496289947000L, 600.0, 1800.0, 3600f, 3L }, new Object[] { 1496289948000L, 100.0, 200.0, 9600f, 2L }, new Object[] { 1496289949000L, 80.0, 160.0, 1600f, 2L }, new Object[] { 1496289950000L, 60.0, 120.0, 700f, 2L });
SiddhiTestHelper.waitForEvents(100, 4, inEventCount, 60000);
AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Remove events matched", true, SiddhiTestHelper.isEventsMatch(removeEventsList, expected));
AssertJUnit.assertEquals("Number of success events", 4, inEventCount.get());
AssertJUnit.assertEquals("Number of remove events", 4, removeEventCount.get());
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.
the class AggregationTestCase method incrementalStreamProcessorTest38.
@Test(dependsOnMethods = { "incrementalStreamProcessorTest37" })
public void incrementalStreamProcessorTest38() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest38");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp string);";
String query = "" + "@BufferSize('3') " + "@IgnoreEventsOlderThanBuffer('true')" + "define aggregation stockAggregation " + "from stockStream " + "select avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue " + "aggregate by timestamp every sec...year; " + "define stream inputStream (symbol string, value int, startTime string, " + "endTime string, perValue string); " + "@info(name = 'query1') " + "from inputStream join stockAggregation " + "within \"2017-06-01 09:35:00 +05:30\", \"2017-06-01 10:37:57 +05:30\" " + "per perValue " + "select avgPrice, totalPrice as sumPrice, lastTradeValue " + "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 (add 5.30 to get corresponding IST time)
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, "2017-06-01 04:05:50" });
// Thursday, June 1, 2017 4:05:51 AM
stockStreamInputHandler.send(new Object[] { "IBM", 50f, 60f, 90L, 6, "2017-06-01 04:05:51" });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56, "2017-06-01 04:05:52" });
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16, "2017-06-01 04:05:52" });
// Thursday, June 1, 2017 4:05:50 AM (out of order. must be processed with 1st event for 50th second)
stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10, "2017-06-01 04:05:50" });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, "2017-06-01 04:05:54" });
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96, "2017-06-01 04:05:54" });
// Thursday, June 1, 2017 4:05:50 AM (out of order. should not be processed since events for 50th second is
// no longer in the buffer and @IgnoreEventsOlderThanBuffer is true)
stockStreamInputHandler.send(new Object[] { "IBM", 50f, 60f, 90L, 6, "2017-06-01 04:05:50" });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, "2017-06-01 04:05:56" });
stockStreamInputHandler.send(new Object[] { "IBM", 500f, null, 200L, 7, "2017-06-01 04:05:56" });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, "2017-06-01 04:06:56" });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 600f, null, 200L, 6, "2017-06-01 04:07:56" });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 700f, null, 200L, 20, "2017-06-01 05:07:56" });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM", 1, "2017-06-01 09:35:51 +05:30", "2017-06-01 09:35:52 +05:30", "minutes" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { 214.44444444444446, 1930.0, 3500f }, new Object[] { 400.0, 400.0, 3600f }, new Object[] { 700.0, 700.0, 14000f }, new Object[] { 600.0, 600.0, 3600f });
SiddhiTestHelper.waitForEvents(100, 4, inEventCount, 60000);
AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Remove events matched", true, SiddhiTestHelper.isEventsMatch(removeEventsList, expected));
AssertJUnit.assertEquals("Number of success events", 4, inEventCount.get());
AssertJUnit.assertEquals("Number of remove events", 4, removeEventCount.get());
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.
the class ComplexPatternTestCase method testQuery6.
@Test
public void testQuery6() throws InterruptedException {
log.info("testPatternComplex6 - OUT 1");
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 query = "" + "@info(name = 'query1') " + "from every e1 = Stream1 -> e2 = Stream2 [e1.symbol != 'AMBA' ] <2:> -> e3 = Stream2 [volume <= 70 ] " + "select e3.symbol as symbol1, e2[0].symbol as symbol2, e3.volume as volume3 " + "insert into StockQuote;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query1", 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", "GOOG", 65 }, event.getData());
break;
case 2:
AssertJUnit.assertArrayEquals(new Object[] { "IBN", "DDD", 70 }, event.getData());
break;
default:
AssertJUnit.assertSame(2, inEventCount);
}
}
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
});
InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
siddhiAppRuntime.start();
stream1.send(new Object[] { "IBM", 75.6f, 105 });
Thread.sleep(100);
stream2.send(new Object[] { "GOOG", 21f, 51 });
stream2.send(new Object[] { "FBX", 21f, 81 });
stream2.send(new Object[] { "WSO2", 176.6f, 65 });
stream1.send(new Object[] { "BIRT", 21f, 81 });
stream1.send(new Object[] { "AMBA", 126.6f, 165 });
stream2.send(new Object[] { "DDD", 23f, 181 });
stream2.send(new Object[] { "BIRT", 21f, 86 });
stream2.send(new Object[] { "IBN", 21f, 70 });
stream2.send(new Object[] { "WSO2", 176.6f, 90 });
stream1.send(new Object[] { "AMBA", 126.6f, 165 });
stream2.send(new Object[] { "DOX", 16.2f, 25 });
Thread.sleep(100);
AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.
the class ComplexPatternTestCase method testQuery2.
@Test
public void testQuery2() throws InterruptedException {
log.info("testPatternComplex2 - OUT 1");
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 query = "" + "@info(name = 'query1') " + "from every ( e1=Stream1[price > 20] -> e2=Stream1[price > 20]<1:2>) -> e3=Stream1[price > e1.price] " + "select e1.price as price1, e2[0].price as price2_0, e2[1].price as price2_1, e3.price as price3 " + "insert into OutputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query1", 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[] { 55.6f, 54.0f, 53.6f, 57.0f }, event.getData());
break;
default:
AssertJUnit.assertSame(1, inEventCount);
}
}
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
});
InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
siddhiAppRuntime.start();
stream1.send(new Object[] { "WSO2", 55.6f, 100 });
Thread.sleep(100);
stream1.send(new Object[] { "GOOG", 54f, 100 });
Thread.sleep(100);
stream1.send(new Object[] { "WSO2", 53.6f, 100 });
Thread.sleep(100);
stream1.send(new Object[] { "GOOG", 57f, 100 });
Thread.sleep(100);
AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations