use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testMultipleStreamsFromWindow.
@Test
public void testMultipleStreamsFromWindow() throws InterruptedException {
log.info("Test joining a single window by multiple streams");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockInputStream (symbol string, price float, volume long); " + "define stream SummaryOfCompanyTriggerStream (symbol string); " + "define stream VolumeGreaterThanTriggerStream (volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(10); ";
String query = "" + "@info(name = 'query0') " + "from StockInputStream " + "insert into StockWindow; " + "@info(name = 'query1') " + "from StockWindow join SummaryOfCompanyTriggerStream#window.length(1) " + "on StockWindow.symbol == SummaryOfCompanyTriggerStream.symbol " + "select StockWindow.symbol, sum(StockWindow.price) as totalPrice, sum(StockWindow.volume) as volumes " + "insert into SummaryOfCompanyOutputStream; " + "" + "@info(name = 'query2') " + "from StockWindow join VolumeGreaterThanTriggerStream#window.length(1) " + "on StockWindow.volume > VolumeGreaterThanTriggerStream.volume " + "select StockWindow.symbol, StockWindow.price, StockWindow.volume as volume " + "insert into VolumeGreaterThanOutputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("SummaryOfCompanyOutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
Object[] data = events[0].getData();
if ("WSO2".equals(data[0])) {
assertArrayEquals(new Object[] { "WSO2", 430.0, 57L }, data);
} else if ("IBM".equals(data[0])) {
assertArrayEquals(new Object[] { "IBM", 222.0, 16L }, data);
}
}
});
siddhiAppRuntime.addCallback("VolumeGreaterThanOutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
assertEquals("Error in number of events with volume > 6", 6, events.length);
}
});
siddhiAppRuntime.start();
InputHandler stockInputStreamInputHandler = siddhiAppRuntime.getInputHandler("StockInputStream");
InputHandler summaryOfCompanyTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("SummaryOfCompanyTriggerStream");
InputHandler volumeGreaterThanTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("VolumeGreaterThanTriggerStream");
// 10 inputs
stockInputStreamInputHandler.send(new Object[] { "WSO2", 84.0f, 20L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 90.0f, 1L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 55.0f, 5L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 77.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 46.0f, 5L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 36.0f, 2L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 6L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 86.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 26.0f, 6L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 96.0f, 8L });
// out of 10
stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 36.0f, 15L });
Thread.sleep(100);
summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "WSO2" });
summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "IBM" });
volumeGreaterThanTriggerStreamInputHandler.send(new Object[] { 5L });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testUnidirectionalJoin.
@Test
public void testUnidirectionalJoin() throws InterruptedException {
log.info("Test unidirectional join with two windows");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); " + "define window cseEventWindow (symbol string, price float, volume int) lengthBatch(2); " + "define window twitterWindow (user string, tweet string, company string) lengthBatch(2); ";
String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from twitterStream " + "insert into twitterWindow; " + "" + "@info(name = 'query2') " + "from cseEventWindow unidirectional join twitterWindow " + "on cseEventWindow.symbol == twitterWindow.company " + "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " + "insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
try {
siddhiAppRuntime.addCallback("cseEventWindow", new StreamCallback() {
@Override
public void receive(Event[] events) {
System.out.print("cseEventWindow: ");
EventPrinter.print(events);
}
});
siddhiAppRuntime.addCallback("twitterWindow", new StreamCallback() {
@Override
public void receive(Event[] events) {
System.out.print("twitterWindow: ");
EventPrinter.print(events);
}
});
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[] { "WSO2", 55.6f, 100 });
cseEventStreamHandler.send(new Object[] { "IBM", 59.6f, 100 });
twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
twitterStreamHandler.send(new Object[] { "User2", "Hello World2", "WSO2" });
cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(500);
cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
Thread.sleep(1000);
AssertJUnit.assertEquals(2, inEventCount);
AssertJUnit.assertEquals(0, removeEventCount);
AssertJUnit.assertTrue(eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
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.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow9.
@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();
}
use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow5.
@Test
public void testLengthBatchWindow5() throws InterruptedException {
final int length = 2;
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(" + length + ") output " + "expired events; ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow " + "select symbol,price,volume " + "insert expired events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
for (Event event : events) {
count++;
AssertJUnit.assertEquals("Remove event order", count, event.getData(2));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 1 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
inputHandler.send(new Object[] { "IBM", 700f, 3 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
inputHandler.send(new Object[] { "IBM", 700f, 5 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
Thread.sleep(500);
AssertJUnit.assertEquals("Remove event count", 4, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations