Apache flink Flink CheckpointedFunction的用例

Apache flink Flink CheckpointedFunction的用例,apache-flink,flink-streaming,Apache Flink,Flink Streaming,在浏览Flink官方文档时,我遇到了CheckpointedFunction。 想知道为什么和什么时候使用这个函数。我目前正在从事一项有状态的Flink作业,该作业严重依赖于ProcessFunction在RocksDB中保存状态。只是想知道CheckpointFunction是否比ProcessFunctionCheckpointFunction更适用于需要处理应由Flink管理并包含在检查点中的状态的情况,但是,如果您没有使用KeyedStream,因此无法像在KeyedProcessFun

在浏览Flink官方文档时,我遇到了
CheckpointedFunction

想知道为什么和什么时候使用这个函数。我目前正在从事一项有状态的Flink作业,该作业严重依赖于
ProcessFunction
在RocksDB中保存状态。只是想知道
CheckpointFunction
是否比
ProcessFunction

CheckpointFunction
更适用于需要处理应由Flink管理并包含在检查点中的状态的情况,但是,如果您没有使用KeyedStream,因此无法像在
KeyedProcessFunction
中那样使用KeyedState


CheckpointedFunction
最常见的用例是在源和汇中。

CheckpointedFunction
用于需要处理应由Flink管理并包含在检查点中的状态的情况,但是,如果您没有使用KeyedStream,因此无法像在
KeyedProcessFunction
中那样使用KeyedState


CheckpointedFunction
最常见的用例是在源和汇中。

除了@David的答案之外,我还有另一个用例,我不使用
CheckpointedFunction
。我确实在一个
ProcessFunction
中使用它,我想(以编程方式)计算我的作业重新启动了多少次。我使用
MyProcessFunction
CheckpointedFunction
并在作业重新启动时更新
ListState重新启动。我在集成测试中使用此状态,以确保在出现故障时重新启动作业。我的例子是建立在这个基础上的

公共类MyProcessFunction扩展ProcessFunction实现CheckpointedFunction{
...
私有瞬态重新启动;
@凌驾
public void快照状态(FunctionSnapshotContext上下文)引发异常{…}
@凌驾
public void initializeState(FunctionInitializationContext上下文)引发异常{
restarts=context.getOperatorStateStore().getListState(新的ListStateDescriptor(“restarts”,Long.class));
if(context.isRestored()){
List restoreList=Lists.newArrayList(restarts.get());
if(restoreList==null | | restoreList.isEmpty()){
重新启动。添加(1L);
System.out.println(“重新启动:1”);
}否则{
Long max=Collections.max(restoreList);
System.out.println(“重新启动:+max”);
重新启动。添加(最大+1);
}
}否则{
System.out.println(“重新启动:从未恢复”);
}
}
@凌驾
公共void open(配置参数)引发异常{…}
@凌驾
public void processElement(V值、上下文ctx、收集器输出)引发异常{…}
@凌驾
public void onTimer(长时间戳、OnTimerContext ctx、收集器输出)引发异常{…}
}

除了@David的答案之外,我还有另一个用例,在这个用例中,我不使用
CheckpointedFunction
接收器
。我确实在一个
ProcessFunction
中使用它,我想(以编程方式)计算我的作业重新启动了多少次。我使用
MyProcessFunction
CheckpointedFunction
并在作业重新启动时更新
ListState重新启动。我在集成测试中使用此状态,以确保在出现故障时重新启动作业。我的例子是建立在这个基础上的

公共类MyProcessFunction扩展ProcessFunction实现CheckpointedFunction{
...
私有瞬态重新启动;
@凌驾
public void快照状态(FunctionSnapshotContext上下文)引发异常{…}
@凌驾
public void initializeState(FunctionInitializationContext上下文)引发异常{
restarts=context.getOperatorStateStore().getListState(新的ListStateDescriptor(“restarts”,Long.class));
if(context.isRestored()){
List restoreList=Lists.newArrayList(restarts.get());
if(restoreList==null | | restoreList.isEmpty()){
重新启动。添加(1L);
System.out.println(“重新启动:1”);
}否则{
Long max=Collections.max(restoreList);
System.out.println(“重新启动:+max”);
重新启动。添加(最大+1);
}
}否则{
System.out.println(“重新启动:从未恢复”);
}
}
@凌驾
公共void open(配置参数)引发异常{…}
@凌驾
public void processElement(V值、上下文ctx、收集器输出)引发异常{…}
@凌驾
public void onTimer(长时间戳、OnTimerContext ctx、收集器输出)引发异常{…}
}
public class MyProcessFunction<V> extends ProcessFunction<V, V> implements CheckpointedFunction {
    ...

    private transient ListState<Long> restarts;

    @Override
    public void snapshotState(FunctionSnapshotContext context) throws Exception { ... }

    @Override
    public void initializeState(FunctionInitializationContext context) throws Exception {
        restarts = context.getOperatorStateStore().getListState(new ListStateDescriptor<Long>("restarts", Long.class));

        if (context.isRestored()) {
            List<Long> restoreList = Lists.newArrayList(restarts.get());
            if (restoreList == null || restoreList.isEmpty()) {
                restarts.add(1L);
                System.out.println("restarts: 1");
            } else {
                Long max = Collections.max(restoreList);
                System.out.println("restarts: " + max);
                restarts.add(max + 1);
            }
        } else {
            System.out.println("restarts: never restored");
        }
    }

    @Override
    public void open(Configuration parameters) throws Exception { ... }

    @Override
    public void processElement(V value, Context ctx, Collector<V> out) throws Exception { ... }

    @Override
    public void onTimer(long timestamp, OnTimerContext ctx, Collector<V> out) throws Exception { ... }
}