import com.espertech.esper.client.*; public class test { public static void main(String[] args) throws InterruptedException { Configuration configuration = new Configuration(); configuration.configure(); EPServiceProvider svcProvider = EPServiceProviderManager.getDefaultProvider(configuration); EPRuntime runtime = svcProvider.getEPRuntime(); EPAdministrator epAdmin = svcProvider.getEPAdministrator(); // This will trigger a SensorEvent for every result from the MySQL query, for every TriggerQryEvent // The ORDER BY is critical, because that determines the order in which the SensorEvents are // posted. epAdmin.createEPL("create schema TriggerQryEvent as (triggerId int)"); epAdmin.createEPL( "insert into SensorEvent " + "select sqldata.* from " + "sql:mydb['SELECT " + " UNIX_TIMESTAMP(Record_Time)*1000 + MICROSECOND(Record_Time)/1000 as RecordTime, " + " d.Sensor_ID, Sensor_Name, Value " + "FROM fdr_FDRData AS d join fdr_SensorLU AS s on d.Sensor_ID = s.Sensor_ID " + "WHERE aircraft_ID = 18 "+ "ORDER BY Record_Time'] sqldata, " + "TriggerQryEvent "); // Create MachEvents for those SensorEvents that match the Sensor_Name we want .. // Being able to do this efficiently is a key difference between using Esper & // doing straight-up database queries, which we could easily do for this simple // example, but would fall apart as we add more and more complex event queries. epAdmin.createEPL( "insert into MachEvent " + "select * from SensorEvent(Sensor_Name = 'Mach')"); // Find all 14 second windows having min(Value) >= .7. With this query, 2 consecutive // windows that satisify the condition will return 2 separate events. // ****** We don't do anything w/ the output of this query; it's just an example ****** epAdmin.createEPL( "insert into OverSpeedEvent " + "select min(Value), max(Value), min(RecordTime), max(RecordTime) as mt " + "from MachEvent.win:ext_timed(RecordTime, 14 sec) " + "having min(Value) >= .7"); // The above query releases a flurry of events, when really there's only 1 long one. // This one will detect the transition; still not really what we want, but closer: // ****** We don't do anything w/ the output of this query; it's just an example ****** epAdmin.createEPL( "select * from pattern [ every (ee=MachEvent(Value >= .7) -> MachEvent(Value < .7)) ]"); // This one finally does what we want. EPStatement epl = epAdmin.createEPL( "select startTime, startVal, endTime, endVal, endTime-startTime as durationMillis " + "from MachEvent match_recognize ( " + "measures S[0].RecordTime as startTime, S[0].Value as startVal," + " E.RecordTime as endTime, E.Value as endVal " + "pattern ( S+? E ) " + "define S as S.Value >= .77, E as not E.Value >= .77 " + ") having endTime-startTime > 5000"); // This adds something to the last statement to handle the detected events .. // all we do is print them to stdout. epl.addListener(new UpdateListener() { @Override public void update(EventBean[] newEvents, EventBean[] oldEvents) { System.out.println(newEvents[0].getUnderlying()); } }); // This triggers the TriggerQryEvent event, which will cause the // SQL query to be executed and each row to by posted as a SensorEvent. // There's probably a way to do this w/ pure EPL, but I didn't find it quickly. EventSender sender = runtime.getEventSender("TriggerQryEvent"); sender.sendEvent(new java.util.HashMap()); } }