Flink使用托管算子状态ListCheckpointed

要使用托管算子状态,有状态函数可以实现更通用的CheckpointedFunction接口或ListCheckpointed<T extends Serializable>接口。官方参考链接:https://ci.apache.org/projects/flink/flink-docs-release-1.10/dev/stream/state/state.html#listcheckpointed

  • 此文使用ListCheckpointed,如下是官网的案例
class CounterSource
  extends RichParallelSourceFunction[Long]
    with ListCheckpointed[Long] {

  @volatile
  private var isRunning = true

  private var offset = 0L

  override def run(ctx: SourceFunction.SourceContext[Long]): Unit = {
    val lock = ctx.getCheckpointLock

    while (isRunning) {
      // output and state update are atomic
      lock.synchronized({
        ctx.collect(offset)

        offset += 1
      })
    }
  }

  override def cancel(): Unit = isRunning = false

  override def restoreState(state: util.List[Long]): Unit =
    for (s <- state) {
      offset = s
    }

  override def snapshotState(checkpointId: Long, timestamp: Long): util.List[Long] =
    Collections.singletonList(offset)

}
  • 上述官网案例再导入Long的时候会报Scala的Long类型不是java.io.Serializable的下界的错误
  • 错误如下
Error:(62, 10) type arguments [Long] do not conform to trait ListCheckpointed's type parameter bounds [T <: java.io.Serializable]
    with ListCheckpointed[Long] {
  • 只需要使用将Scala的Long改成Java类型的Long即可~!
    在代码里加入 import java.lang.{Long => JavaLong}
  • 修改后的代码如下:
import java.lang.{Long => JavaLong}

class CounterSource
  extends RichParallelSourceFunction[Long]
    with ListCheckpointed[JavaLong] {   //此处修改为JavaLang

  @volatile
  private var isRunning = true

  private var offset = 0L

  override def run(ctx: SourceFunction.SourceContext[Long]): Unit = {
    val lock = ctx.getCheckpointLock

    while (isRunning) {
      // output and state update are atomic
      lock.synchronized({
        ctx.collect(offset)

        offset += 1
      })
    }
  }

  override def cancel(): Unit = isRunning = false

  override def restoreState(state: util.List[JavaLong]): Unit = //此处修改为JavaLang

    for (s <- state) {
      offset = s
    }

  override def snapshotState(checkpointId: Long, timestamp: Long): util.List[JavaLong] =   //此处修改为JavaLang

    Collections.singletonList(offset)

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值