Java Code Examples for io.siddhi.core.event.Event#getData()
The following examples show how to use
io.siddhi.core.event.Event#getData() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTrpSourceMapper.java From siddhi with Apache License 2.0 | 6 votes |
@Override protected void mapAndProcess(Object eventObject, InputEventHandler inputEventHandler) throws InterruptedException { if (eventObject != null) { if (eventObject instanceof Event) { Event output = new Event(streamDefinition.getAttributeList().size()); for (AttributeMapping attributeMapping : attributeMappingList) { output.getData()[attributeMapping.getPosition()] = ((Event) eventObject).getData( Integer.parseInt(attributeMapping.getMapping())); } inputEventHandler.sendEvent(output); } else { throw new SiddhiAppRuntimeException("Event object must be either Event[], Event or Object[] " + "but found " + eventObject.getClass().getCanonicalName()); } } }
Example 2
Source File: JsonSourceMapper.java From siddhi-map-json with Apache License 2.0 | 5 votes |
private Event processCustomEvent(ReadContext readContext) { Configuration conf = Configuration.defaultConfiguration(); Event event = new Event(streamAttributesSize); Object[] data = event.getData(); Object childObject = readContext.read(DEFAULT_ENCLOSING_ELEMENT); readContext = JsonPath.using(conf).parse(childObject); Gson gsonWithNull = new GsonBuilder().serializeNulls().create(); for (MappingPositionData mappingPositionData : this.mappingPositions) { int position = mappingPositionData.getPosition(); Object mappedValue; try { mappedValue = readContext.read(mappingPositionData.getMapping()); if (mappedValue == null) { data[position] = null; } else if (mappedValue instanceof Map) { data[position] = attributeConverter.getPropertyValue(gsonWithNull.toJson(mappedValue), streamAttributes.get(position).getType()); } else { data[position] = attributeConverter.getPropertyValue(mappedValue.toString(), streamAttributes.get(position).getType()); } } catch (PathNotFoundException e) { if (failOnMissingAttribute) { log.error("Json message " + childObject.toString() + " contains missing attributes. Hence dropping the message."); return null; } data[position] = null; } } return event; }
Example 3
Source File: InputEventHandler.java From siddhi with Apache License 2.0 | 5 votes |
public void sendEvent(Event event) throws InterruptedException { try { if (latencyTracker != null && Level.BASIC.compareTo(siddhiAppContext.getRootMetricsLevel()) <= 0) { latencyTracker.markOut(); } Object[] transportProperties = trpProperties.get(); trpProperties.remove(); String[] transportSyncProperties = trpSyncProperties.get(); trpSyncProperties.remove(); if (event.getTimestamp() == -1) { long currentTimestamp = timestampGenerator.currentTime(); event.setTimestamp(currentTimestamp); } for (int i = 0; i < transportMapping.size(); i++) { AttributeMapping attributeMapping = transportMapping.get(i); event.getData()[attributeMapping.getPosition()] = AttributeConverter.getPropertyValue( transportProperties[i], attributeMapping.getType()); } inputEventHandlerCallback.sendEvent(event, transportSyncProperties); eventCount++; } catch (RuntimeException e) { LOG.error(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + " Error in applying transport property mapping for '" + sourceType + "' source at '" + inputHandler.getStreamId() + "' stream.", e); } finally { trpProperties.remove(); trpSyncProperties.remove(); } }
Example 4
Source File: TemplateBuilder.java From siddhi with Apache License 2.0 | 5 votes |
public Object build(Event event) { if (isObjectMessage) { return event.getData()[objectIndex]; } else { return formatMessage(event.getData()); } }
Example 5
Source File: JsonSinkMapper.java From siddhi-map-json with Apache License 2.0 | 5 votes |
private JsonObject constructSingleEventForDefaultMapping(Event event) { Object[] data = event.getData(); JsonObject jsonEventObject = new JsonObject(); JsonObject innerParentObject = new JsonObject(); String attributeName; Object attributeValue; Gson gson = new Gson(); for (int i = 0; i < data.length; i++) { attributeName = attributeNameArray[i]; attributeValue = data[i]; if (attributeValue != null) { if (attributeValue.getClass() == String.class) { innerParentObject.addProperty(attributeName, attributeValue.toString()); } else if (attributeValue instanceof Number) { innerParentObject.addProperty(attributeName, (Number) attributeValue); } else if (attributeValue instanceof Boolean) { innerParentObject.addProperty(attributeName, (Boolean) attributeValue); } else if (attributeValue instanceof Map) { if (!((Map) attributeValue).isEmpty()) { innerParentObject.add(attributeName, gson.toJsonTree(attributeValue)); } } } } jsonEventObject.add(EVENT_PARENT_TAG, innerParentObject); return jsonEventObject; }
Example 6
Source File: TestSinkOptionSinkMapper.java From siddhi with Apache License 2.0 | 5 votes |
@Override public void mapAndSend(Event event, OptionHolder optionHolder, Map<String, TemplateBuilder> payloadTemplateBuilderMap, SinkListener sinkListener) { String topic = topicOption.getValue(event); if (mapType) { for (TemplateBuilder templateBuilder : payloadTemplateBuilderMap.values()) { sinkListener.publish(new Event(System.currentTimeMillis(), new Object[]{sinkType, topic, templateBuilder.isObjectMessage() + "-" + templateBuilder.getType().toString()})); } } else { //to change the topic as topic is picked from the original event event.getData()[0] = sinkType; sinkListener.publish(new Event(System.currentTimeMillis(), new Object[]{sinkType, topic, prefix})); } }
Example 7
Source File: DelayWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(expectedExceptions = SiddhiAppCreationException.class) public void delayWindowTest8() { log.info("DelayWindow Test8 : Testing delay window for dinamic value"); SiddhiManager siddhiManager = new SiddhiManager(); String eventStream = "" + "define stream CargoStream (weight int); " + "define stream OutputStream(weight int, totalWeight long, averageWeight double); "; String query = "" + "@info(name='CargoWeightQuery') " + "from CargoStream#window.delay(1/2) " + "select weight, sum(weight) as totalWeight, avg(weight) as averageWeight " + "insert into OutputStream;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(eventStream + query); siddhiAppRuntime.addCallback("CargoWeightQuery", 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; } }); StreamCallback callBack = new StreamCallback() { @Override public void receive(Event[] events) { for (Event event : events) { lastValue = (Long) event.getData(1); averageValue = (Double) event.getData(2); } } }; siddhiAppRuntime.addCallback("OutputStream", callBack); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream"); siddhiAppRuntime.start(); try { inputHandler.send(new Object[]{1000}); inputHandler.send(new Object[]{1500}); SiddhiTestHelper.waitForEvents(100, 2, count, 3000); AssertJUnit.assertEquals(2, inEventCount); AssertJUnit.assertEquals(0, removeEventCount); AssertJUnit.assertEquals(Long.valueOf(2500), lastValue); AssertJUnit.assertEquals(Double.valueOf(1250), averageValue); } catch (Exception e) { log.error(e.getMessage()); } finally { siddhiAppRuntime.shutdown(); } }
Example 8
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest9() throws InterruptedException { log.info("Incremental persistence test 9 - min-max counting with group-by."); final int inputEventCount = 10; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String streams = "" + "@app:name('incrementalPersistenceTest9') " + "define stream TempStream (roomNo long, temp long); " + "define stream MaxTempStream (roomNo long, maxTemp long); "; String query = "" + "@info(name = 'query1') " + "from TempStream#window.length(10) " + "select roomNo, max(temp) as maxTemp " + "group by roomNo " + "insert into MaxTempStream; "; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; if (inEvents != null) { for (Event event : inEvents) { count++; inEventsList.add(event.getData()); inEventCount.incrementAndGet(); lastValue = (Long) event.getData(1); } eventArrived = true; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } } }; try { siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler stockStream = siddhiAppRuntime.getInputHandler("TempStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { stockStream.send(new Object[]{i, 55L + i}); } //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); siddhiAppRuntime.addCallback("query1", queryCallback); stockStream = siddhiAppRuntime.getInputHandler("TempStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 1, 1000L}); //persisting siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 2, 20L}); siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{4, 50L}); //AssertJUnit.assertTrue(count <= (inputEventCount + 3)); AssertJUnit.assertEquals(new Long(59), lastValue); AssertJUnit.assertEquals(true, eventArrived); } finally { siddhiAppRuntime.shutdown(); } }
Example 9
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest4() throws InterruptedException { log.info("Incremental persistence test 4 - time batch window query"); final int inputEventCount = 10; final int eventWindowSizeInSeconds = 2; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest4') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream"; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = (Long) inEvent.getData(2); log.info("last value: " + lastValue); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { inputHandler.send(new Object[]{"IBM", 75.6f + i, 100}); } Thread.sleep(4000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(1000), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); inputHandler.send(new Object[]{"WSO2", 100.4f, 150}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 200.4f, 110}); inputHandler.send(new Object[]{"IBM", 300.4f, 100}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 400.4f, 300}); inputHandler.send(new Object[]{"IBM", 500.6f, 120}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 600.6f, 400}); Thread.sleep(100); siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(50); inputHandler.send(new Object[]{"IBM", 700.6f, 230}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 800.6f, 125}); inputHandler.send(new Object[]{"IBM", 900.6f, 370}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 1000.6f, 140}); //shutdown Siddhi app Thread.sleep(5000); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 10)); AssertJUnit.assertEquals(new Long(2045), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 10
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest3() throws InterruptedException { log.info("Incremental persistence test 3 - time window query"); final int inputEventCount = 10; final int eventWindowSizeInSeconds = 2; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest3') " + "@app:playback " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream"; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = (Long) inEvent.getData(2); log.info("last value: " + lastValue); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { inputHandler.send(i, new Object[]{"IBM", 75.6f + i, 100}); } Thread.sleep(4000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(1000), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); inputHandler.send(3000, new Object[]{"WSO2", 100.4f, 150}); //Thread.sleep(100); inputHandler.send(3010, new Object[]{"WSO2", 200.4f, 110}); inputHandler.send(3020, new Object[]{"IBM", 300.4f, 100}); //Thread.sleep(100); inputHandler.send(3030, new Object[]{"WSO2", 400.4f, 300}); inputHandler.send(3040, new Object[]{"IBM", 500.6f, 120}); Thread.sleep(10); inputHandler.send(3050, new Object[]{"WSO2", 600.6f, 400}); Thread.sleep(100); siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); try { //loading siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(500); inputHandler.send(3500, new Object[]{"IBM", 700.6f, 230}); Thread.sleep(10); inputHandler.send(3540, new Object[]{"WSO2", 800.6f, 125}); inputHandler.send(3590, new Object[]{"IBM", 900.6f, 370}); Thread.sleep(10); inputHandler.send(3600, new Object[]{"WSO2", 1000.6f, 140}); //shutdown Siddhi app Thread.sleep(5000); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 10)); AssertJUnit.assertEquals(new Long(2045), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 11
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest2() throws InterruptedException { log.info("Incremental persistence test 2 - length batch window query"); final int inputEventCount = 10; final int eventWindowSize = 4; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest2') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(" + eventWindowSize + ") " + "select symbol, price, sum(volume) as totalVol " + "insert all events into OutStream"; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = (Long) inEvent.getData(2); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { inputHandler.send(new Object[]{"IBM", 75.6f + i, 100}); } Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(400), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); inputHandler.send(new Object[]{"WSO2", 100.4f, 150}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 200.4f, 110}); inputHandler.send(new Object[]{"IBM", 300.4f, 100}); //Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 400.4f, 300}); inputHandler.send(new Object[]{"IBM", 500.6f, 120}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 600.6f, 400}); Thread.sleep(100); siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e); } siddhiAppRuntime.start(); Thread.sleep(500); inputHandler.send(new Object[]{"IBM", 700.6f, 230}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 800.6f, 125}); inputHandler.send(new Object[]{"IBM", 900.6f, 370}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 1000.6f, 140}); //shutdown Siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= (inputEventCount + 6)); AssertJUnit.assertEquals(new Long(865), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 12
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest8() throws InterruptedException { log.info("Incremental persistence test 8 - min-max counting."); final int inputEventCount = 10; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String streams = "" + "@app:name('incrementalPersistenceTest8') " + "define stream TempStream (roomNo long, temp long); " + "define stream MaxTempStream (roomNo long, maxTemp long); "; String query = "" + "@info(name = 'query1') " + "from TempStream#window.length(10) " + "select roomNo, max(temp) as maxTemp " + "insert into MaxTempStream; "; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; if (inEvents != null) { for (Event event : inEvents) { count++; inEventsList.add(event.getData()); inEventCount.incrementAndGet(); lastValue = (Long) event.getData(1); } eventArrived = true; } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; } } }; try { siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler stockStream = siddhiAppRuntime.getInputHandler("TempStream"); siddhiAppRuntime.start(); for (int i = 0; i < inputEventCount; i++) { stockStream.send(new Object[]{i, 55L + i}); } //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); siddhiAppRuntime.addCallback("query1", queryCallback); stockStream = siddhiAppRuntime.getInputHandler("TempStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 1, 1000L}); //persisting siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 2, 20L}); siddhiAppRuntime.persist(); Thread.sleep(2000); stockStream.send(new Object[]{inputEventCount + 3, 30L}); AssertJUnit.assertTrue(count <= (inputEventCount + 3)); AssertJUnit.assertEquals(new Long(1000), lastValue); AssertJUnit.assertEquals(true, eventArrived); } finally { siddhiAppRuntime.shutdown(); } }
Example 13
Source File: IncrementalPersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void incrementalPersistenceTest12() throws InterruptedException { log.info("Incremental file persistence test 12 - length window query with max attribute aggregator"); final int eventWindowSize = 5; SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath)); String siddhiApp = "" + "@app:name('incrementalPersistenceTest12') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream#window.length(" + eventWindowSize + ") " + "select symbol, price, max(volume) as maxVol " + "insert into OutStream "; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = new Long((Integer) inEvent.getData(2)); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 500}); inputHandler.send(new Object[]{"IBM", 75.6f, 200}); inputHandler.send(new Object[]{"IBM", 75.6f, 300}); inputHandler.send(new Object[]{"IBM", 75.6f, 250}); inputHandler.send(new Object[]{"IBM", 75.6f, 150}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(500L), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(5000); //persisting for the second time to store the inc-snapshot siddhiAppRuntime.persist(); Thread.sleep(100); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { log.error(e.getMessage(), e); Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } siddhiAppRuntime.start(); Thread.sleep(5000); inputHandler.send(new Object[]{"IBM", 100.4f, 280}); AssertJUnit.assertEquals((Long) 300L, lastValue); inputHandler.send(new Object[]{"WSO2", 200.4f, 150}); AssertJUnit.assertEquals((Long) 300L, lastValue); inputHandler.send(new Object[]{"IBM", 300.4f, 200}); AssertJUnit.assertEquals((Long) 280L, lastValue); inputHandler.send(new Object[]{"WSO2", 400.4f, 270}); AssertJUnit.assertEquals((Long) 280L, lastValue); inputHandler.send(new Object[]{"WSO2", 400.4f, 280}); AssertJUnit.assertEquals((Long) 280L, lastValue); //shutdown Siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(true, eventArrived); }
Example 14
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest4") public void persistenceTest5() throws InterruptedException { log.info("persistence test 5 - window restart expired event "); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(10 sec) " + "select symbol, price, sum(volume) as totalVol " + "insert all events into OutStream "; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; if (inEvents != null) { for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); firstValue = (Long) inEvent.getData(2); } } if (removeEvents != null) { for (Event removeEvent : removeEvents) { count++; lastValue = (Long) removeEvent.getData(2); } } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(firstValue, 200); //persisting Thread.sleep(500); siddhiAppRuntime.persist(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } //shutdown siddhi app Thread.sleep(15000); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(400, firstValue); AssertJUnit.assertEquals(null, lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 15
Source File: DelayWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(description = "Check whether aggregations are done correctly when using delay window ") public void delayWindowTest5() { log.info("DelayWindow Test5 : Testing delay window for Aggregation"); SiddhiManager siddhiManager = new SiddhiManager(); String eventStream = "" + "define stream CargoStream (weight int); " + "define stream OutputStream(weight int, totalWeight long, averageWeight double); "; String query = "" + "@info(name='CargoWeightQuery') " + "from CargoStream#window.delay(1 sec) " + "select weight, sum(weight) as totalWeight, avg(weight) as averageWeight " + "insert into OutputStream;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(eventStream + query); siddhiAppRuntime.addCallback("CargoWeightQuery", 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; } }); StreamCallback callBack = new StreamCallback() { @Override public void receive(Event[] events) { for (Event event : events) { lastValue = (Long) event.getData(1); averageValue = (Double) event.getData(2); } } }; siddhiAppRuntime.addCallback("OutputStream", callBack); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream"); siddhiAppRuntime.start(); try { inputHandler.send(new Object[]{1000}); Thread.sleep(100); inputHandler.send(new Object[]{1500}); SiddhiTestHelper.waitForEvents(100, 2, count, 3000); AssertJUnit.assertEquals(2, inEventCount); AssertJUnit.assertEquals(0, removeEventCount); AssertJUnit.assertEquals(Long.valueOf(2500), lastValue); AssertJUnit.assertEquals(Double.valueOf(1250), averageValue); } catch (Exception e) { log.error(e.getMessage()); } finally { siddhiAppRuntime.shutdown(); } }
Example 16
Source File: SessionWindowTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(description = "Check whether aggregations are done correctly with session window") public void testSessionWindow20() throws InterruptedException { log.info("SessionWindow Test20: testing aggregations"); SiddhiManager siddhiManager = new SiddhiManager(); String streams = "" + "define stream purchaseEventStream (user string, item_number int, price double, quantity int); "; String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(1 sec, user) " + "select user, avg(price) as avgPrice, sum(price) as totPrice group by user " + "insert into outputStream;"; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); siddhiAppRuntime.addCallback("query0", new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); if (inEvents != null) { inEventCount = inEventCount + inEvents.length; count.addAndGet(inEvents.length); } if (removeEvents != null) { removeEventCount = removeEventCount + removeEvents.length; count.addAndGet(removeEvents.length); } eventArrived = true; } }); StreamCallback callBack = new StreamCallback() { @Override public void receive(Event[] events) { for (Event event : events) { innerAssertionsPassed = false; averageValue = (Double) event.getData(1); totalValue = (Double) event.getData(2); innerAssertionsPassed = true; } } }; siddhiAppRuntime.addCallback("outputStream", callBack); InputHandler purchaseEventStreamHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream"); siddhiAppRuntime.start(); purchaseEventStreamHandler.send(new Object[]{"user0", 101, 34.4, 5}); purchaseEventStreamHandler.send(new Object[]{"user0", 102, 24.5, 2}); purchaseEventStreamHandler.send(new Object[]{"user1", 101, 5.0, 5}); purchaseEventStreamHandler.send(new Object[]{"user1", 102, 6.0, 2}); SiddhiTestHelper.waitForEvents(100, 2, count, 4200); AssertJUnit.assertEquals(4, inEventCount); AssertJUnit.assertEquals(0, removeEventCount); AssertJUnit.assertTrue(Double.valueOf(29.45) == averageValue || Double.valueOf(5.5) == averageValue); AssertJUnit.assertTrue(Double.valueOf(58.9) == totalValue || Double.valueOf(11.0) == totalValue); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertTrue(innerAssertionsPassed); siddhiAppRuntime.shutdown(); }
Example 17
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test public void persistenceTest1() throws InterruptedException { log.info("persistence test 1 - window query"); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(10) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream "; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = (Long) inEvent.getData(2); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(200), lastValue); //persisting Thread.sleep(500); siddhiAppRuntime.persist(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e); } inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(10); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertTrue(count <= 6); AssertJUnit.assertEquals(new Long(400), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 18
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest3") public void persistenceTest4() throws InterruptedException { log.info("persistence test 4 - window restart"); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(10 sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream "; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timestamp, inEvents, removeEvents); eventArrived = true; for (Event inEvent : inEvents) { count++; AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); lastValue = (Long) inEvent.getData(2); } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); Thread.sleep(100); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(200), lastValue); //persisting Thread.sleep(500); siddhiAppRuntime.persist(); inputHandler.send(new Object[]{"IBM", 75.6f, 100}); Thread.sleep(100); inputHandler.send(new Object[]{"WSO2", 75.6f, 100}); //restarting siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } //shutdown siddhi app Thread.sleep(500); siddhiAppRuntime.shutdown(); AssertJUnit.assertEquals(new Long(400), lastValue); AssertJUnit.assertEquals(true, eventArrived); }
Example 19
Source File: JsonSourceMapper.java From siddhi-map-json with Apache License 2.0 | 4 votes |
private Event[] convertToEventArrayForDefaultMapping(Object eventObject) { Gson gson = new Gson(); JsonObject[] eventObjects = gson.fromJson(eventObject.toString(), JsonObject[].class); Event[] events = new Event[eventObjects.length]; int index = 0; JsonObject eventObj; for (JsonObject jsonEvent : eventObjects) { if (jsonEvent.has(DEFAULT_JSON_EVENT_IDENTIFIER)) { eventObj = jsonEvent.get(DEFAULT_JSON_EVENT_IDENTIFIER).getAsJsonObject(); if (failOnMissingAttribute && eventObj.size() < streamAttributes.size()) { log.error("Json message " + eventObj.toString() + " contains missing attributes. " + "Hence dropping the message."); continue; } } else { eventObj = jsonEvent; if (eventObj.size() < streamAttributes.size()) { log.error("Json message " + eventObj.toString() + " is not in an accepted format for default " + "mapping. Hence dropping the message."); continue; } } Event event = new Event(streamAttributes.size()); Object[] data = event.getData(); int position = 0; for (Attribute attribute : streamAttributes) { String attributeName = attribute.getName(); Attribute.Type type = attribute.getType(); JsonElement attributeElement = eventObj.get(attributeName); if (attributeElement == null) { data[position++] = null; } else { data[position++] = attributeConverter.getPropertyValue( attributeElement.getAsString(), type); } } events[index++] = event; } return Arrays.copyOfRange(events, 0, index); }
Example 20
Source File: PersistenceTestCase.java From siddhi with Apache License 2.0 | 4 votes |
@Test(dependsOnMethods = "persistenceTest10") public void persistenceTest11() throws InterruptedException { log.info("persistence test 11 - batch window query"); PersistenceStore persistenceStore = new InMemoryPersistenceStore(); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setPersistenceStore(persistenceStore); String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume long );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(2) " + "select *" + "insert all events into OutStream "; QueryCallback queryCallback = new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { EventPrinter.print(timeStamp, inEvents, removeEvents); eventArrived = true; atomicCount.incrementAndGet(); for (Event inEvent : inEvents) { AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0))); } if (removeEvents != null) { for (Event removeEvent : removeEvents) { lastValue = (Long) removeEvent.getData(2); } } } }; SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); inputHandler.send(new Object[]{"IBM", 75.6f, 100L}); inputHandler.send(new Object[]{"WSO2", 75.6f, 101L}); inputHandler.send(new Object[]{"IBM", 75.6f, 102L}); inputHandler.send(new Object[]{"IBM", 75.6f, 103L}); SiddhiTestHelper.waitForEvents(100, 2, atomicCount, 10000); AssertJUnit.assertTrue(eventArrived); AssertJUnit.assertEquals(new Long(101), lastValue); //persisting siddhiAppRuntime.persist(); Thread.sleep(500); inputHandler.send(new Object[]{"WSO2", 75.6f, 50L}); inputHandler.send(new Object[]{"IBM", 75.6f, 50L}); inputHandler.send(new Object[]{"IBM", 75.6f, 50L}); //restarting execution plan Thread.sleep(500); siddhiAppRuntime.shutdown(); siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); siddhiAppRuntime.addCallback("query1", queryCallback); inputHandler = siddhiAppRuntime.getInputHandler("StockStream"); siddhiAppRuntime.start(); //loading try { siddhiAppRuntime.restoreLastRevision(); } catch (CannotRestoreSiddhiAppStateException e) { Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed"); } inputHandler.send(new Object[]{"IBM", 75.6f, 100L}); //shutdown siddhi app Thread.sleep(500); SiddhiTestHelper.waitForEvents(100, 3, atomicCount, 10000); AssertJUnit.assertEquals(new Long(103), lastValue); siddhiAppRuntime.shutdown(); }