Esper计算模式中的重复事件

时间:2016-06-15 13:34:43

标签: aggregate esper cumulocity

我有一系列事件如下:

  1. 事件A启动序列
  2. 发生多个事件
  3. 事件C停止序列
  4. 我用一种模式做到了[每个A - >; (B到C)],它似乎是正确的(你怎么看?)。但我正在努力从序列中出现的B事件中检索和汇总信息。我想简单地计算并且可能有一些平均值但似乎没有任何效果(example1返回1,示例2返回0,示例3返回null,即使我的B事件存在)。

    insert into CreateMeasurement
    select
        C.source            as source,
        "carDrivingAnalyse" as type,
        C.time              as time,
        {
            "example1", count(*),
            "example2", count(B),
            "example3", B.countOf()
        } as fragments
    
    from pattern [
        every A = EventCreated(
            type = "Ignition",
            getString(A, "Ignition.status") = "ON")
    
        -> (
            B = EventCreated(
                type = "DrivingEvent",
                source = A.source,
                getString(B, "DrivingEvent.prop1") = getString(A, "Ignition.prop1"),
                getNumber(B, "DrivingEvent.prop2") = getNumber(A, "Ignition.prop2"))
    
            until C = EventCreated(
                type = "Ignition",
                getString(C, "Ignition.status") = "OFF",
                source = A.source,
                getString(C, "Ignition.prop1") = getString(A, "Ignition.prop1"),
                getNumber(C, "Ignition.prop2") = getNumber(A, "Ignition.prop2"))
        )
    ]
    

2 个答案:

答案 0 :(得分:0)

我会为此用例选择不同的方法,并使用上下文而不是模式:

create context EventContext
  context PartionBySource
    partition by event.source.value from EventCreated,
  context ContextBorders
    initiated by EventCreated(type="EventA") as startEvent 
    terminated by EventCreated(event.type="EventC");

它已经是一个更复杂的上下文,因为它使用了两个嵌套的上下文。这是Cumulocity文档(http://cumulocity.com/guides/event-language/advanced/#contexts)中上下文的一些基本内容,但对于此高级用法,我建议使用更详细的esper文档http://www.espertech.com/esper/release-5.3.0/esper-reference/html/context.html

总之,这个背景是这样的:

基于EventCreated流,它将首先按其源设备对事件进行分区。每次有EventA时它都会创建一个新的上下文分区(仅当此时此设备没有上下文分区时)。随着EventC的到来,该设备的上下文分区将结束。

现在您可以使用此上下文,例如:

context EventContext
select
  count(*) as count,
  avg(getNumber(e, "speed")) as speed
from EventCreated e
where e.event.type = "EventB"
output last when terminated;

此语句使用上下文。它将在上下文分区结束时输出结果(因此当EventC到达时)。它只会输出最后一个事件(此处没有最后一个关键字,它将输出分区处于活动状态时收到的每个事件)。 在此示例中,它将输出在上下文分区存在期间接收的事件数以及EventB中“speed”片段的平均值。

请注意,此处没有必要按来源进行任何过滤,因为上下文已经按来源对EventCreated上的事件进行了分区。

答案 1 :(得分:0)

我不是专家,但“count()”永远不会返回“null”。在这方面你的观察可能是错误的。 “count()”是一个聚合函数,用于计算from子句中任何内容的出现次数。对于模式,它计算模式匹配的数量。

“count(B)”计算表达式中的(不同的)非空值的数量,请参阅doc。这也不会返回“null”。

“example3”就是你想要的,只要确保根据你的测试数据确实有多个B事件在C事件之前到达。