如何在Apache Flink中发生事件时启动计时器?

huangapple go评论44阅读模式
英文:

How to start a timer when an event occurs in Apache Flink?

问题

我想在Flink中接收到事件时启动计时器,定期发送计时器值,并在接收到另一个事件时停止计时器。让我解释一下。一个事件包括事件名称、源ID和其他字段。所以我有类似这样的内容:

E1("A",1,...) -> E2("B",1,...) -> E3("C",1,...)。

当我接收到事件"A"时,我想启动一个计时器(由源ID分组),并定期更新一个接收计时器值的接收器。当我接收到事件"C"时,我想停止计时器并用最终的计时器值更新接收器。是否有一种方法在Apache Flink中实现这个目标?

英文:

I want to start a timer when an event is received in Flink, send the timer value and stop the timer when another event is received. Let me explain. An event consists of an event name, a source id and other fields. So I have something like this:

E1("A",1,...) -> E2("B",1,...) -> E3("C",1,...).

When I receive event "A" I want to start a timer (keyed by the source id) and update a sink with the timer value periodically. When I receive event "C" I want to stop the timer and update the sink with the final timer value. Is there a way to accomplish that in Apache Flink?

答案1

得分: 1

你可以使用.keyBy(r -> r.getSourceId()),然后跟上自定义的KeyedProcessWindow。这样你就可以访问Flink TimerService,在那里你可以创建定时器。当定时器触发时,你的自定义函数中的onTimer()方法将被调用,在这里你可以使用状态中保存的最后一个值来更新“sink”(实际上是某种远程服务)。你可以在onTimer()方法中启动新的定时器,以实现周期性更新。

英文:

You'd do a .keyBy(r -> r.getSourceId()), and follow that with a custom KeyedProcessWindow. This gives you access to the Flink TimerService, where you can create timers. When a timer fires, the onTimer() method in your custom function will be called, which is where you can use the last value you saved in state to update the "sink" (actually a remote service of some sort). You can start a new timer in the onTimer() method, so that it's a periodic update.

huangapple
  • 本文由 发表于 2023年2月9日 02:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定