英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论