如何在esper中记录事件的到达时间

时间:2013-06-09 16:39:05

标签: time esper

我想在esper中记录数据处理时间,我选择Bollinger Band作为示例。在布林带,有一个叫做移动平均线(MA)。 MA从计算股票价格平均值的结果中获得。在这种情况下,我设置win:length(20)。因此,MA可以从数据窗口视图中存在的20个事件的平均股票价格计算结果中获得。以下是我创建的代码。

public class BollingerBand {
    static double startTime, finishTime;

    public static void main (String [] args){
        Configuration configuration = new Configuration();
        configuration.addEventType("Stock", Stock.class);

        EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(configuration);
        AdapterInputSource source = new AdapterInputSource("BollingerBand.csv");

        EPStatement statement = epService.getEPAdministrator().createEPL("insert into Aggregation " +
                "select prevcount(symbol), symbol, avg(price) as SimpleMovingAverage, stddev(price) as StandardDeviation, " +
                "last(price) as price, last(timestamp) as date from Stock.std:groupwin(symbol).win:length(20)" +
                " group by symbol having count(*) >=20");

        statement.addListener(new UpdateListener() {

            public void update(EventBean[] newEvents, EventBean[] oldEvents) {
                // TODO Auto-generated method stub
                //System.out.println("Event Receive : "+newEvents[0].getUnderlying());
                startTime = System.currentTimeMillis();
                System.out.println("\nStart time : " + startTime + " miliseconds\n");
            }
        });

        EPStatement statement2 = epService.getEPAdministrator().createEPL("select symbol, " +
                 "SimpleMovingAverage + 2*StandardDeviation as UpperBand," +
                 "SimpleMovingAverage as MiddleBand," +
                 "SimpleMovingAverage - 2*StandardDeviation as LowerBand," +
                 "price," +
                 "4*StandardDeviation/SimpleMovingAverage as Bandwidth," +
                 "(price - (SimpleMovingAverage - (2 * StandardDeviation))) / ((SimpleMovingAverage + " +
                 "(2 * StandardDeviation)) - (SimpleMovingAverage - (2 * StandardDeviation))) as PercentB," +
                 "date from Aggregation");

        statement2.addListener(new UpdateListener() {

            public void update(EventBean[] newEvents, EventBean[] oldEvents) {
                // TODO Auto-generated method stub
                //System.out.println("Event Receive : "+newEvents[0].getUnderlying());
                finishTime = System.currentTimeMillis();
                System.out.println("Start time : " + startTime + " miliseconds");
                System.out.println("Finish time : " + finishTime + " miliseconds");
                System.out.println("Processing time : " + (finishTime-startTime) + " miliseconds");
            }
        });

        (new CSVInputAdapter(epService, source, "Stock")).start();
    }

}

从上面的代码中,如果计算出平均值,将记录时间。但我需要的是我希望在第20个事件和下一个事件进入数据窗口视图时记录时间。它是从布林带计算结果获得的开始时间和结束时间。我的问题是如何记录第20个事件的时间,同时下一个事件进入窗口视图数据。请帮忙

2 个答案:

答案 0 :(得分:0)

发送事件时,CSV适配器不提供回调。但是,您可以轻松更改其代码。或者您可以使用不同的CSV阅读器并通过运行时API发送事件。

答案 1 :(得分:0)

也许有某种TickCounter,其中有一个带有键值对(item_count和timestamp)的映射。您可以在第二个UpdateListener中更新它,当然您总是可以查找键20的项目。

顺便说一下,我使用了布林带计算,但使用了Storm和EsperBolt。在这里博客: http://chanchal.wordpress.com/2014/07/08/using-esperbolt-and-storm-to-calculate-bollinger-bands/