use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class DistinctCountAggregationTestCase method incrementalStreamProcessorTest1.
@Test
public void incrementalStreamProcessorTest1() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest1: testing distinctCount incremental aggregator");
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 distinctCount(symbol) as distinctCnt " + "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 \"days\" " + "select AGG_TIMESTAMP, s.distinctCnt " + "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", 70f, null, 40L, 10, 1496289950000L });
// 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:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM1", 102f, 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[] { "IBM1", 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[] { "IBM2", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
// Thursday, June 1, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 61f, 44f, 200L, 56, 1496297276000L });
// Friday, June 2, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 801f, null, 100L, 10, 1496383676000L });
// Saturday, June 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 901f, null, 100L, 15, 1496470076000L });
// Monday, July 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 96, 1499062076000L });
// Thursday, August 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 402f, null, 200L, 9, 1501740476000L });
// Friday, August 3, 2018 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 63f, 44f, 200L, 6, 1533276476000L });
// Saturday, August 3, 2019 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 260f, 44f, 200L, 16, 1564812476000L });
// Monday, August 3, 2020 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 26f, 44f, 200L, 16, 1596434876000L });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { 1496275200000L, 8 }, new Object[] { 1496361600000L, 1 }, new Object[] { 1496448000000L, 1 }, new Object[] { 1499040000000L, 1 }, new Object[] { 1501718400000L, 1 }, new Object[] { 1533254400000L, 1 }, new Object[] { 1564790400000L, 1 }, new Object[] { 1596412800000L, 1 });
SiddhiTestHelper.waitForEvents(100, 8, inEventCount, 10000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 8, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Number of remove events", 8, removeEventCount.get());
AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isEventsMatch(removeEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class PurgingTestCase method incrementalPurgingTest2.
@Test
public void incrementalPurgingTest2() throws InterruptedException {
LOG.info("incrementalPurgingTest2");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = " define stream stockStream (symbol string, price float, lastClosingPrice float," + " volume long ,quantity int);";
String query = " @purge(enable='true',interval='5 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'" + " ,days='all',months='all',years='all')) " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue " + "group by symbol " + "aggregate every sec...years ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
siddhiAppRuntime.start();
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96 });
Thread.sleep(1000);
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
EventPrinter.print(events);
AssertJUnit.assertNotNull("Initial queried events cannot be null", events);
AssertJUnit.assertEquals("Number of events before purging", 6, events.length);
Thread.sleep(120100);
events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
EventPrinter.print(events);
AssertJUnit.assertNull("No events expected after purging", events);
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class PurgingTestCase method incrementalPurgingTestCase3.
@Test
public void incrementalPurgingTestCase3() throws InterruptedException {
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='true',interval='10 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'," + "days='all',months='all',years='all')) " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:51 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289951011L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289952000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289950000L });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 500f, null, 200L, 7, 1496289951011L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:53 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289953000L });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289953000L });
Thread.sleep(2000);
Event[] events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
EventPrinter.print(events);
AssertJUnit.assertEquals("Check time windows", 7, events.length);
List<Object[]> eventsList = new ArrayList<>();
for (Event event : events) {
eventsList.add(event.getData());
}
List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 50.0 }, new Object[] { 1496289950000L, "IBM", 900.0 }, new Object[] { 1496289951000L, "IBM", 100.0 }, new Object[] { 1496289951000L, "WSO2", 500.0 }, new Object[] { 1496289952000L, "IBM", 400.0 }, new Object[] { 1496289953000L, "IBM", 100.0 }, new Object[] { 1496289953000L, "WSO2", 100.0 });
AssertJUnit.assertTrue("Data Matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsList, expected));
Thread.sleep(120000);
events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
EventPrinter.print(events);
Assert.assertNull(events);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class LatestAggregationTestCase method latestTestCase4.
@Test(dependsOnMethods = "latestTestCase3")
public void latestTestCase4() throws InterruptedException {
LOG.info("latestTestCase4: 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, sum(s.avgPrice) as totalAvg " + "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;
}
}
});
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, 1496289950010L });
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, 1496289952010L });
stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952020L });
// 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, 1496289954010L });
stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954020L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956010L });
stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956030L });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { "WSO22", 750f, 65.0 }, new Object[] { "WSO24", 1600f, 80.0 }, new Object[] { "IBM1", 3500f, 801.5 });
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));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.stream.input.InputHandler in project siddhi by wso2.
the class LatestAggregationTestCase method latestTestCase3.
@Test(dependsOnMethods = "latestTestCase2")
public void latestTestCase3() throws InterruptedException {
LOG.info("latestTestCase3: testing latest incremental aggregator with another agg");
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 AGG_TIMESTAMP, s.symbol, s.latestPrice, s.avgPrice " + "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[] { 1496289950000L, "WSO22", 750f, 65.0 }, new Object[] { 1496289952000L, "WSO24", 1600f, 80.0 }, new Object[] { 1496289954000L, "IBM1", 10200f, 101.5 }, new Object[] { 1496289956000L, "IBM1", 3500f, 700.0 });
SiddhiTestHelper.waitForEvents(100, 4, inEventCount, 10000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 4, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Number of remove events", 4, removeEventCount.get());
AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isUnsortedEventsMatch(removeEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
Aggregations