【发布时间】:2018-08-10 15:31:06
【问题描述】:
我有一堆输入流,它们可能会随着时间的推移发送更新。如果发生更新,我需要计算增量以便能够进一步处理它。简而言之:
Input: 10 -> State: 10, Output: 10
Input: 12 -> State: 12, Output: 2
Input: 5 -> State: 5, Output: -7
我阅读了 stateful processing 和 timely processing 以了解如何在 Apache Beam 应用程序中处理这种状态,但我不知道的是:
- 是否 100% 保证我的有状态 DoFn 不会并行处理具有相同键的项目?
- 我想确保在我的应用程序重新启动或失败时保持状态,以便我可以从正确的初始值开始。如何确保我的 DoFn 在关闭之前“清理”(保留到数据存储区)?
对于#2,我想知道这在使用全局窗口时是否可行:
public class Delta extends DoFn<KV<String, Integer>, Integer> {
@StateId("state")
private final StateSpec<ValueState<Integer>> stateSpec = StateSpecs.value();
@TimerId("timer")
private final TimerSpec timerSpec = TimerSpecs.timer(TimeDomain.EVENT_TIME);
@ProcessElement
public void process(ProcessContext context,
BoundedWindow window,
@StateId("state") ValueState<Integer> state,
@TimerId("timer") Timer myTimer) {
// Assign the timer to the end of the current window, which is a global window
// Not sure if this always triggers when the application stops...
myTimer.set(window.maxTimestamp());
int value = context.element().getValue();
int acc = getOrInitialize(state.read());
int delta = value - acc;
state.write(value);
context.output(delta);
}
@OnTimer("timer")
public void onTimer(OnTimerContext context,
@StateId("state") ValueState<Integer> state) {
// Persist value of state here
}
private int getOrInitialize(Integer a) {
// Get initial value of state here
return (a != null) ? a : 0;
}
}
【问题讨论】:
标签: java apache-beam