Search code examples
espernesper

Forcing output from an esper stream on expression


I've got an esper query for pulling market data. It looks like

select * from L1Quote(Symbol='MSFT')

This will fire when ever a new quote occurs. I require a first quote/event to do some initial setup. This is no problem for symbols where events/quotes fire every second but for some symbols you won't get a new quote event for a minute.

Working under the assumption that there have been quote events before this expressions is setup. Is there a way to force output when the expression is first parsed? ie is there a way to say, give me the last event when the expression starts?


Solution

  • You just need to create a window to store the latest quote and then query against it.

    In you Esper EPL:

    //the L1Quote event definition
    create schema L1Quote(Symbol string, ...)
    
    //create a window with the same definition as L1Quote and it retain the latest event by Symbol
    create window L1Quotes.std:unique(Symbol) as select * from L1Quote
    
    //insert all incoming quotes into the window we created
    insert into L1Quotes select * from L1Quote
    

    Your client will need to both query the window and subscribe to it:

    //subscribe to quote updates
    EPStatement statement = epAdmin.createEPL("select * from L1Quotes(Symbol='MSFT')");
    statement.addListener([my listener]);
    
    //get the latest quote
    epEngine.executeQuery("select * from L1Quotes(Symbol='MSFT')");