StreamInsight在一个时间范围内获取最新事件

时间:2012-05-11 17:59:57

标签: streaminsight

我想考虑基于有效负载的最新事件,即StockTicksData id,并忽略时间窗内的任何重复

有效负载和查询

var StockTicksData = new[]
{
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 01), Price = 100, ID = "000361105"    }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 02), Price = 200, ID = "000361105" }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 03), Price = 3000, ID = "000361105" }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 04), Price = 100, ID = "001055102" }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 05), Price = 700, ID = "001084102" }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 06), Price = 500, ID = "001084102" }, 
new { Timestamp = new DateTime(2012, 5, 9, 1, 00, 07), Price = 100, ID = "001084102" }, 
};
var stocks = StockTicksData.ToPointStream(Application, t =>
PointEvent.CreateInsert(t.Timestamp, t),
AdvanceTimeSettings.IncreasingStartTime);

var query = (from e in stocks
        group e by e.ID into ipGroup
        from win in ipGroup.TumblingWindow(TimeSpan.FromSeconds(2), 
            HoppingWindowOutputPolicy.ClipToWindowEnd)
        select new 
        {   
             CusipID = cusipGroup.Key,
             Timestamp = win.Max(e => e.Timestamp),
             Price = 0
        });

var cusipIdGroupCepStream = (from px in query
                         join lz in stocks
                         on new { px.CusipID, px.Timestamp }
                         equals new { lz.CusipID, lz.Timestamp }
                         select new 
                           {
                              CusipId = lz.CusipID,
                              Price = lz.Price,
                              TimeofArrival = lz.Timestamp
                           });

上面的查询工作正常,但是当我使用输入适配器时,我必须插入一个cti事件来刷新输出。这是代码

Ticks Generator 它有到达时间

priceTick.TimeofArrival = DateTime.Now.AddTicks(1);

输入Adpator

.... In a loop
{
currEvent = CreateInsertEvent();
currEvent.StartTime = priceTick.TimeofArrival; **// each event has time arrival from input which is t+1**
currEvent.Payload = new PriceTick { Id = priceTick.Id, Price = priceTick.Price, TimeofArrival = priceTick.TimeofArrival };
  pendingEvent = null;

 Enqueue(ref currEvent);

 // Also send an CTI event
 EnqueueCtiEvent(priceTick.TimeofArrival.AddTicks(1)); **// Added to flush the output**
 }

相同的查询未提供输入适配器和cti事件的预期输出

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

以上查询在LinqPad中有效,因为没有要生成的CTI事件。使用输入/输出适配器进行配置时,相同的查询不会吐出完整的结果集。

为了刷新您需要配置的输出,在输入工厂的via ITypedDeclareAdvanceTimeProperties中生成CTI事件。它负责根据设置生成CTI事件。我使用以下配置

public AdapterAdvanceTimeSettings DeclareAdvanceTimeProperties(StockTickerInputConfig configInfo,EventShape eventShape){return new AdapterAdvanceTimeSettings(new AdvanceTimeGenerationSettings(configInfo.CtiFrequency,TimeSpan.FromTicks(1)),AdvanceTimePolicy.Adjust); }

在Point事件中,每秒数据的频率非常高,它会丢弃/不会将事件清除到输出适配器。我必须手动排队LAST CTI事件以确保将结果刷新到输出适配器。

输入工厂中的当前设置不会在我的最后一个数据馈送/事件入队后生成最后一个CTI。我在输入适配器中创建了一个条件来检查这是否是要排队的最后一个事件,然后通过EnqueueCtiEvent将cti事件排入队列

如果需要,我可以提供代码。让我知道有人有更好的方法来解决这个问题。