use of org.ballerinalang.siddhi.core.SiddhiManager 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.SiddhiManager 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.SiddhiManager in project ballerina by ballerina-lang.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow12.
@Test
public void testExternalTimeBatchWindow12() throws InterruptedException {
log.info("ExternalTimeBatchWindow test12");
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 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(0, removeEventCount);
assertTrue(eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.ballerinalang.siddhi.core.SiddhiManager 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.SiddhiManager in project ballerina by ballerina-lang.
the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow14.
@Test
public void testExternalTimeBatchWindow14() throws Exception {
log.info("ExternalTimeBatchWindow test14");
SiddhiManager siddhiManager = new SiddhiManager();
String query = "define stream jmxMetric(cpu int, timestamp long); " + "define window jmxMetricWindow(cpu int, timestamp long) externalTimeBatch(timestamp, 10 sec);" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name='query1') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, count(1) as count insert into tmp;";
SiddhiAppRuntime runtime = siddhiManager.createSiddhiAppRuntime(query);
final AtomicInteger recCount = new AtomicInteger(0);
runtime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
assertEquals(1, inEvents.length);
recCount.incrementAndGet();
double avgCpu = (Double) inEvents[0].getData()[0];
if (recCount.get() == 1) {
assertEquals(15, avgCpu, 0);
} else if (recCount.get() == 2) {
assertEquals(85, avgCpu, 0);
}
long count = (Long) inEvents[0].getData()[1];
assertEquals(3, count);
}
});
InputHandler input = runtime.getInputHandler("jmxMetric");
runtime.start();
// external events' time stamp less than the window, should not have event recieved in call back.
long now = 0;
int length = 3;
for (int i = 0; i < length; i++) {
input.send(new Object[] { 15, now + i * 10 });
}
// if the trigger event mix with the last window, we should see the avgValue is not expected
for (int i = 0; i < length; i++) {
// the first entity of the second round
input.send(new Object[] { 85, now + 10000 + i * 10 });
}
// to trigger second round
input.send(new Object[] { 10000, now + 10 * 10000 });
Thread.sleep(1000);
assertEquals(2, recCount.get());
}
Aggregations