1

I create a table that store the latest financial ticks, I would like to atomically select and calculate some ticks that meeting certain conditions immediately after updating the tick table.

Is there any method in Esper can achieve this requirement?


Thanks to user650839. I wrote a demo following his instruction. The demo uses named window instead of table.

Here defines the financial tick event:

public class Tick {

    private String symbol;
    private double bid;
    private double ask;
    private String datetime;

    // Constructor
    ......

    // getter / setter
    ......
}

Here shows the EPL statements that update and select from the named window atomically:

// create the named window
create window tickWin#unique(symbol) as select * from Tick

// on-merge clause
on Tick as nt 
merge tickWin as wt 
where wt.symbol = nt.symbol 
when not matched 
    then insert select * 
when matched 
    then update set wt.bid = nt.bid, wt.ask = nt.ask, wt.datetime = nt.datetime 
    then insert into UpdatedTick select * 

// the output stream
select * from UpdatedTick 

Here shows the listener, add this listener to the output stream mentioned above, simply print out the updated tick event.

public class TickListener implements UpdateListener {

    @Override
    public void update(EventBean[] newEvents, EventBean[] oldEvents) {

        if (newEvents == null || newEvents.length ==0) return;

        System.out.println(newEvents[0]);
    }
}
jdnull
  • 11
  • 3
  • 1
    Please have a look at https://stackoverflow.com/help/how-to-ask and then edit your question. – Yeti Jul 18 '18 at 04:17

1 Answers1

0

You can use on-merge. It has multiple actions

on <something>
merge <some named window or table>
where <some correlation>
when matched then
  update set <some value>
  insert into OutputStream select <some values>
user650839
  • 2,594
  • 1
  • 13
  • 9
  • Thanks bro, you save my day. I write a demo and find this works.BTW, I find that there should add 'then' before 'insert' as well in Esper 7.1.0. – jdnull Jul 18 '18 at 06:19
  • I notice that this sentence in the reference, "Similarly you should avoid simultaneous update and delete actions for the same match: the engine does not guarantee whether the update or the delete take final affect". It seems to be contradicting to other sentence in the reference, "the engine evaluates and executes all actions listed under the same matched-clause in order". I don't quite understand. – jdnull Jul 18 '18 at 07:26