use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.
the class CustomJoinWindowTestCase method testGroupByUseCase.
@Test
public void testGroupByUseCase() throws InterruptedException {
log.info("Test joining a single window by multiple streams");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream SensorStream (name string, value float, roomNo int, deviceID string); " + "define window SensorWindow (name string, value float, roomNo int, deviceID string) timeBatch(1 " + "second); ";
String query = "" + "@info(name = 'query0') " + "from SensorStream " + "insert into SensorWindow; " + "@info(name = 'query1') " + "from SensorWindow " + "select name, max(value) as maxValue, roomNo " + "group by name, roomNo " + "insert into MaxSensorReadingPerRoomStream; " + "@info(name = 'query2') " + "from SensorWindow " + "select name, max(value) as maxValue " + "group by name " + "insert into OverallMaxSensorReadingStream; " + "@info(name = 'query3') " + "from SensorWindow " + "select name, avg(value) as avgValue " + "group by name " + "insert into OverallAverageSensorReadingStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("MaxSensorReadingPerRoomStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
System.out.print("MaxSensorReadingPerRoomStream: ");
EventPrinter.print(events);
}
});
siddhiAppRuntime.addCallback("OverallMaxSensorReadingStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
System.out.print("OverallMaxSensorReadingStream: ");
EventPrinter.print(events);
}
});
siddhiAppRuntime.addCallback("OverallAverageSensorReadingStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
System.out.print("OverallAverageSensorReadingStream: ");
EventPrinter.print(events);
}
});
siddhiAppRuntime.start();
InputHandler sensorStreamInputHandler = siddhiAppRuntime.getInputHandler("SensorStream");
sensorStreamInputHandler.send(new Object[] { "Temperature", 23.0f, 1, "T001A" });
sensorStreamInputHandler.send(new Object[] { "Pressure", 20.0f, 1, "P001" });
sensorStreamInputHandler.send(new Object[] { "Temperature", 25.0f, 2, "T002" });
sensorStreamInputHandler.send(new Object[] { "Temperature", 24.0f, 3, "T003" });
sensorStreamInputHandler.send(new Object[] { "Pressure", 45.0f, 3, "P003" });
sensorStreamInputHandler.send(new Object[] { "Temperature", 23.5f, 1, "T001B" });
sensorStreamInputHandler.send(new Object[] { "Humidity", 15.0f, 2, "H002" });
sensorStreamInputHandler.send(new Object[] { "Humidity", 10.0f, 3, "H003" });
Thread.sleep(1000);
Thread.sleep(500);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow17.
@Test
public void testExternalTimeBatchWindow17() throws InterruptedException {
log.info("ExternalTimeBatchWindow test17");
SiddhiManager siddhiManager = new SiddhiManager();
String inputStream = "define stream inputStream(currentTime long,value int); " + "define window inputWindow(currentTime long,value int) externalTimeBatch(currentTime,5 sec,1200); ";
String query = " " + "@info(name = 'query0') " + "from inputStream " + "insert into inputWindow; " + "" + "@info(name='query') " + "from inputWindow " + "select value " + "insert into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + query);
siddhiAppRuntime.addCallback("query", new QueryCallback() {
int count = 0;
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
if (count == 0) {
assertEquals(0L, inEvents[0].getData(0));
assertEquals(11L, inEvents[inEvents.length - 1].getData(0));
}
if (count == 1) {
assertEquals(12L, inEvents[0].getData(0));
}
count += 1;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
for (long i = 0; i < 10000; i += 100) {
inputHandler.send(new Object[] { i + 10000, i / 100 });
Thread.sleep(200);
}
}
use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow11.
@Test
public void testExternalTimeBatchWindow11() throws InterruptedException {
log.info("ExternalTimeBatchWindow test11");
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); ";
String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total " + "insert into uniqueIps ;";
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 (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
for (Event event : inEvents) {
inEventCount++;
if (inEventCount == 1) {
assertEquals(4L, event.getData(2));
} else if (inEventCount == 2) {
assertEquals(7L, event.getData(2));
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
Thread.sleep(2100);
inputHandler.send(new Object[] { 1366335805606L, "192.10.1.7" });
inputHandler.send(new Object[] { 1366335805605L, "192.10.1.8" });
Thread.sleep(2100);
inputHandler.send(new Object[] { 1366335805606L, "192.10.1.91" });
inputHandler.send(new Object[] { 1366335805605L, "192.10.1.92" });
inputHandler.send(new Object[] { 1366335806606L, "192.10.1.9" });
inputHandler.send(new Object[] { 1366335806690L, "192.10.1.10" });
Thread.sleep(3000);
assertEquals("Event arrived", true, eventArrived);
assertEquals("In Events ", 2, inEventCount);
assertEquals("Remove Events ", 0, removeEventCount);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow6.
@Test
public void testExternalTimeBatchWindow6() throws InterruptedException {
log.info("ExternalTimeBatchWindow test6");
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, 3 sec) " + "output all events; ";
String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total " + "insert all events into uniqueIps ;";
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;
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
Thread.sleep(5000);
assertEquals("Event arrived", true, eventArrived);
assertEquals("In Events ", 2, inEventCount);
assertEquals("Remove Events ", 0, removeEventCount);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow13.
@Test
public void testExternalTimeBatchWindow13() throws InterruptedException {
log.info("ExternalTimeBatchWindow test13");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream cseEventStream (timestamp long, symbol string, price float, volume int); " + "define stream twitterStream (timestamp long, user string, tweet string, company string); " + "define window cseEventWindow (timestamp long, symbol string, price float, volume int) " + "externalTimeBatch(timestamp, 1 sec, 0); " + "define window twitterWindow (timestamp long, user string, tweet string, company string) " + "externalTimeBatch(timestamp, 1 sec, 0); ";
String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from twitterStream " + "insert into twitterWindow; " + "" + "@info(name = 'query2') " + "from cseEventWindow join twitterWindow " + "on cseEventWindow.symbol== twitterWindow.company " + "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " + "insert all events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
try {
siddhiAppRuntime.addCallback("query2", 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[] { 1366335804341L, "WSO2", 55.6f, 100 });
twitterStreamHandler.send(new Object[] { 1366335804341L, "User1", "Hello World", "WSO2" });
twitterStreamHandler.send(new Object[] { 1366335805301L, "User2", "Hello World2", "WSO2" });
cseEventStreamHandler.send(new Object[] { 1366335805341L, "WSO2", 75.6f, 100 });
cseEventStreamHandler.send(new Object[] { 1366335806541L, "WSO2", 57.6f, 100 });
Thread.sleep(1000);
assertEquals(2, inEventCount);
assertEquals(1, removeEventCount);
assertTrue(eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
Aggregations