flink MapState 对整个 MapState 实例或 MapState 中每个元素的 TTL

Is the flink MapState's TTL for the whole MapState instance or for each element in the MapState

我在flink中有一个MapState。我将 ttl 设置为 10 分钟。 ttl 是针对整个 MapState 实例还是针对每个元素?

    val ttlConfig = StateTtlConfig
      .newBuilder(Time.minutes(10))
      .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
      .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired)
      .build

      val myMapState: MapState[String, Upload] = ....

      myMapState.put("a", "x")

      // 5 minutes later
      myMapState.put("b", "y")

      // Again 6 minutes later, is the myMapState still available? 
      // I assume myMapState is still available and it still has key "b" for 4 minutes. Is that correct?



您好,您的假设是正确的。

If a TTL is configured and a state value has expired, the stored value will be cleaned up on a best effort basis.

因此即使 TTL 已过,该值仍可能被存储,状态可见性配置将控制是否返回该值。

  • StateTtlConfig.StateVisibility.NeverReturnExpired - 过期值为 一去不复返

  • StateTtlConfig.StateVisibility.ReturnExpiredIfNotCleanedUp - 返回 如果还有的话

有关 state TTL 的更多信息

是的,flink 支持每个条目的 ttl。

State Time-To-Live (TTL) A time-to-live (TTL) can be assigned to the keyed state of any type. If a TTL is ? configured and a state value has expired, the stored value will be cleaned up on a best effort basis which is discussed in more detail below.

All state collection types support per-entry TTLs. This means that list elements and map entries expire independently.

https://ci.apache.org/projects/flink/flink-docs-stable/dev/stream/state/state.html#state-time-to-live-ttl