1

我想使用 FlinkCEP 只对模式进行“惰性”匹配。我怎样才能做到这一点?例如,我有一个输入流 ACABCABCB,我想在 A followBy C 上进行匹配以仅获得 3 个匹配项而不是 6 个匹配项。

我创建了以下示例来说明我的问题。

val env = StreamExecutionEnvironment.createLocalEnvironment(1)
env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime)

case class MyEvent(id: Int, kind: String, value: String)
case class MyAggregatedEvent(id: Int, concatenatedValue: String)

val eventStream = env.fromElements(
  MyEvent(1, "A", "1"), MyEvent(1, "C", "1"),
  MyEvent(1, "A", "2"), MyEvent(1, "B", "1"), MyEvent(1, "C", "2"),
  MyEvent(1, "A", "3"), MyEvent(1, "D", "2"), MyEvent(1, "C", "3"),
  MyEvent(1, "B", "3")
)

val pattern: Pattern[MyEvent, _] = Pattern
  .begin[MyEvent]("pA").where(e => e.kind == "A")
  .next("pC").where(e => e.kind == "C")
  .within(Time.seconds(5))

val patternNextStream: PatternStream[MyEvent] = CEP.pattern(eventStream.keyBy(_.id), pattern)

val outNextStream: DataStream[MyAggregatedEvent] = patternNextStream.flatSelect {
  (pattern: scala.collection.mutable.Map[String, MyEvent], collector: Collector[MyAggregatedEvent]) =>
    val partA = pattern.get("pA").get
    val partC = pattern.get("pC").get

    collector.collect(MyAggregatedEvent(partA.id, partA.value + "=>" + partC.value))
}
outNextStream.print()

env.execute("Experiment")

这给了我以下输出:

MyAggregatedEvent(1,1=>1)

当我将模式更改为:

val pattern: Pattern[MyEvent, _] = Pattern
  .begin[MyEvent]("pA").where(e => e.kind == "A")
  .followedBy("pC").where(e => e.kind == "C")
  .within(Time.seconds(5))

然后打印以下内容:

MyAggregatedEvent(1,1=>1)
MyAggregatedEvent(1,1=>2)
MyAggregatedEvent(1,2=>2)
MyAggregatedEvent(1,1=>3)
MyAggregatedEvent(1,2=>3)
MyAggregatedEvent(1,3 =>3)

如何创建一个只匹配每个事件一次的模式,这样我的输出将是:

我的聚合事件(1,1=>1)
我的聚合事件(1,2=>2)
我的聚合事件(1,3=>3)

4

1 回答 1

1

目前 Flink 的 CEP 库不支持此功能。匹配语义尚无法控制。我认为添加一个MATCH_ALL和一个匹配MATCH_FIRST模式会很好。MATCH_FIRST一旦看到完全匹配的序列,就会丢弃所有中间状态。这应该涵盖您的用例。

于 2016-07-07T08:36:34.113 回答