Apache flink ApacheFlink:定期发出输出记录,即使给定窗口中没有输入记录到达

Apache flink ApacheFlink:定期发出输出记录,即使给定窗口中没有输入记录到达,apache-flink,Apache Flink,我只保留保存在状态中的最后一个事件,并希望定期发出该状态 同样的问题也被解释了,但被接受的答案只给出了使用计时器的提示。这对我不起作用 也发现了,但这是一个不同的问题;那里的输入流在每个窗口中都会有很多事件。我的主要问题是,我的输入流没有针对每个窗口的事件,但我希望为每个窗口生成输出 第一个失败的解决方案: // pseudo code: keyedStream.window(1 Minute).reduce() // does not emit when no events arrive 另

我只保留保存在状态中的最后一个事件,并希望定期发出该状态

同样的问题也被解释了,但被接受的答案只给出了使用计时器的提示。这对我不起作用

也发现了,但这是一个不同的问题;那里的输入流在每个窗口中都会有很多事件。我的主要问题是,我的输入流没有针对每个窗口的事件,但我希望为每个窗口生成输出

第一个失败的解决方案:

// pseudo code:
keyedStream.window(1 Minute).reduce() // does not emit when no events arrive
另一个失败的解决方案:

//Tried with a process function and a timer,
// but timer could only be created in processElement and fires only once.
// it is not periodic

class MyProcessFunction extends KeyedProcessFunction<Integer, Input, Output>{

    public void processElement(Input event, Context context, Collector<Output> out) throws Exception {

        savedState.update(event) // save last event in a state

        // This creates a timer on every event, which is OK
        // and I can easily change the code so that the timer get created only on first event
        // But there was no way to make the timer periodic, independent of arrival of events
        context.timerService().registerEventTimeTimer(someWindow);            
    }

    public void onTimer(long timestamp, OnTimerContext context, Collector<Output> out) throws Exception {
        // this emit exactly once
        out.collect(savedState.value());

        // was hoping for something like 
        // context.timerService().registerEventTimeTimer(timestamp + someWindow);

       // but there is no access to timerService() here, so I couldn't make the timer periodic
    } 
}
//尝试使用进程函数和计时器,
//但计时器只能在processElement中创建,并且只能触发一次。
//它不是周期性的
类MyProcessFunction扩展了KeyedProcessFunction{
公共void processElement(输入事件、上下文、收集器输出)引发异常{
savedState.update(event)//保存状态中的最后一个事件
//这会在每个事件上创建一个计时器,这是正常的
//我可以很容易地更改代码,这样计时器只在第一个事件中创建
//但是没有办法使计时器具有周期性,独立于事件的到达
context.timerService().RegisterEventTimer(someWindow);
}
公共void onTimer(长时间戳、OnTimerContext上下文、收集器输出)引发异常{
//这只会发射一次
out.collect(savedState.value());
//我希望像这样的事情
//context.timerService().RegisterEventTimer(时间戳+someWindow);
//但是这里没有访问timerService()的权限,因此我无法使计时器定期运行
} 
}

有什么建议吗?

您可以在
onTimer
方法中注册计时器;
OnTimerContext
确实有一个
TimerService
。你所希望的是它的正常使用方式


如果您提供更多详细信息,我们可能会找出它不适合您的原因。

我在问题中列出的代码是一个简化版本。在最初的代码中,我使用了reduce,然后是process,不知何故,我把OnTimerContext的上下文搞混了。(天晚了:P)。。。哦,关于“你所希望的是它的正常使用”,这让我很高兴。谢谢