use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.
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.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.
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();
}
}
use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.
the class FrequentWindowEventTbaleTestCase method testFrequentUniqueWindow1.
@Test
public void testFrequentUniqueWindow1() throws InterruptedException {
log.info("FrequentWindow test1");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) frequent(2); ";
String query = "" + "@info(name = 'query0') " + "from purchase[price >= 30] " + "insert into purchaseWindow; " + "" + "@info(name = 'query1') " + "from purchaseWindow " + "select cardNo, price " + "insert all events into PotentialFraud ;";
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 += inEvents.length;
}
if (removeEvents != null) {
removeEventCount += removeEvents.length;
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase");
siddhiAppRuntime.start();
for (int i = 0; i < 2; i++) {
inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
inputHandler.send(new Object[] { "1234-3244-2432-123", 46.36f });
inputHandler.send(new Object[] { "5768-3244-2432-5646", 48.36f });
inputHandler.send(new Object[] { "9853-3244-2432-4125", 78.36f });
}
Thread.sleep(1000);
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
AssertJUnit.assertEquals("In Event count", 8, inEventCount);
AssertJUnit.assertEquals("Out Event count", 6, removeEventCount);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow1.
@Test
public void testLengthBatchWindow1() throws InterruptedException {
log.info("Testing length batch window with no of events smaller than window size");
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(4); ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
AssertJUnit.fail("No events should arrive");
inEventCount = inEventCount + inEvents.length;
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 0 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
Thread.sleep(500);
AssertJUnit.assertEquals(0, inEventCount);
AssertJUnit.assertFalse(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.
the class LenghtBatchWindowTestCase method testLengthBatchWindow7.
@Test
public void testLengthBatchWindow7() throws InterruptedException {
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(4); ";
String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow; " + "@info(name = 'query2') from cseWindow " + "select symbol,sum(price) as sumPrice,volume " + "insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
AssertJUnit.assertEquals("Events cannot be expired", false, removeEvents != null);
for (Event event : inEvents) {
inEventCount++;
if (inEventCount == 1) {
AssertJUnit.assertEquals(100.0, event.getData(1));
} else if (inEventCount == 2) {
AssertJUnit.assertEquals(240.0, event.getData(1));
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 10f, 0 });
inputHandler.send(new Object[] { "WSO2", 20f, 1 });
inputHandler.send(new Object[] { "IBM", 30f, 0 });
inputHandler.send(new Object[] { "WSO2", 40f, 1 });
inputHandler.send(new Object[] { "IBM", 50f, 0 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "WSO2", 60f, 1 });
inputHandler.send(new Object[] { "IBM", 70f, 0 });
inputHandler.send(new Object[] { "WSO2", 80f, 1 });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertEquals(2, inEventCount);
AssertJUnit.assertTrue(eventArrived);
}
Aggregations