【问题标题】:spark streaming understanding timeout setup in mapGroupsWithStatemapGroupsWithState 中的火花流理解超时设置
【发布时间】:2020-11-30 18:06:38
【问题描述】:

在使用 mapGroupsWithState 进行 spark 结构化流式传输时,我正在努力理解超时设置。

下面的链接有非常详细的规范,但我不确定我是否理解正确,尤其是GroupState.setTimeoutTimeStamp() 选项。将状态到期设置为与事件时间相关的含义。 https://spark.apache.org/docs/3.0.0-preview/api/scala/org/apache/spark/sql/streaming/GroupState.html

我在这里复制了它们:

With EventTimeTimeout, the user also has to specify the the the event time watermark in the query using Dataset.withWatermark(). 

With this setting, data that is older than the watermark are filtered out. 
The timeout can be set for a group by setting a timeout timestamp usingGroupState.setTimeoutTimestamp(), and the timeout would occur when the watermark advances beyond the set timestamp. 

You can control the timeout delay by two parameters - watermark delay and an additional duration beyond the timestamp in the event (which is guaranteed to be newer than watermark due to the filtering). 

Guarantees provided by this timeout are as follows:
Timeout will never be occur before watermark has exceeded the set timeout.
Similar to processing time timeouts, there is a no strict upper bound on the delay when the timeout actually occurs. The watermark can advance only when there is data in the stream, and the event time of the data has actually advanced.

问题 1: 这句and the timeout would occur when the watermark advances beyond the set timestamp中的timestamp是什么?它是绝对时间,还是该州当前事件时间的相对持续时间?我知道我可以通过 ```

删除状态来使其过期

例如假设我有一些如下的数据状态,when 会通过在what settings 中设置what value 来过期吗?

+-------+-----------+-------------------+
|expired|something  |          timestamp|
+-------+-----------+-------------------+
|  false|   someKey |2020-08-02 22:02:00|
+-------+-----------+-------------------+

问题 2: 看了Data that is older than the watermark are filtered out这句话,我明白迟到的数据从kafka读取后会被忽略,这样对吗?

问题原因 如果不了解这些,我就无法真正将它们应用到用例中。含义何时使用GroupState.setTimeoutDuration(),何时使用GroupState.setTimeoutTimestamp()

非常感谢。

ps。我也尝试阅读以下内容

-  https://www.waitingforcode.com/apache-spark-structured-streaming/stateful-transformations-mapgroupswithstate/read
(confused me, did not understand)
- https://databricks.com/blog/2017/10/17/arbitrary-stateful-processing-in-apache-sparks-structured-streaming.html
(did not say a lot of it for my interest)

【问题讨论】:

  • 我在mapGroupsWithState[S,U]的签名中发现了这条评论,上面写着@param timeoutConf Timeout configuration for groups that do not receive data for a while.。这让我现在更好地理解了这个概念,所以也许如果我选择GroupState.setTimeoutDuration,将在每个触发器/调用中检查状态超时;如果我选择GroupState.setTimeoutTimestamp(),则只有在收到新事件时才会检查状态时间。

标签: apache-spark spark-structured-streaming


【解决方案1】:

句子and the timeout would occur when the watermark advances beyond the set timestamp中的timestamp是什么?

这是GroupState.setTimeoutTimestamp()设置的时间戳。

是绝对时间还是相对于状态当前事件时间的相对持续时间?

这是基于当前批处理窗口的相对时间(不是持续时间)。

假设我有一些数据状态(列timestamp=2020-08-02 22:02:00),通过在什么设置中设置什么值它什么时候到期?

假设您的接收器查询有一个定义为 5 分钟的处理触发器(由trigger() 设置)。另外,让我们假设您在应用groupByKeymapGroupsWithState 之前使用了水印。我了解您希望使用基于 事件时间 的超时(而不是 处理时间,因此您的查询将类似于:

ds.withWatermark("timestamp", "10 minutes")
  .groupByKey(...) // declare your key
  .mapGroupsWithState(
    GroupStateTimeout.EventTimeTimeout)(
    ...) // your custom update logic

现在,这取决于您如何使用“自定义更新逻辑”设置 TimeoutTimestamp。在您的自定义更新逻辑中的某处,您需要调用

state.setTimeoutTimestamp()

此方法有四种不同的签名,值得仔细阅读它们的文档。由于我们在 (withWatermark) 中设置了水印,我们实际上可以利用这段时间。作为一般规则:将超时时间戳(由state.setTimeoutTimestamp() 设置)设置为大于当前水印的值很重要。为了继续我们的示例,我们添加一小时,如下所示:

state.setTimeoutTimestamp(state.getCurrentWatermarkMs, "1 hour")

总而言之,您的消息可以在22:00:0022:15:00 之间到达您的流,如果该消息是密钥的最后一条消息,它将在您的 GroupState 中超时23:15:00

问题2:读Data that is older than the watermark are filtered out这句话,我理解迟到的数据从kafka读取后被忽略了,对吗?

是的,这是正确的。对于批处理时间间隔 22:00:00 - 22:05:00,所有具有事件时间(由列 timestamp 定义)的消息都将晚于声明的 10 分钟水印(意味着晚于 22:15:00)到达在您的查询中无论如何都会被忽略,并且不会在您的“自定义更新逻辑”中进行处理。

【讨论】:

  • 嗨@mike,试图理解“批处理间隔 22:00:00-22:05:00”。 “批处理间隔”是否与“时钟”时间有关(意味着现在的当前系统时间是多少)?您知道在 spark 读取 kafka 流中,您可以指定“从开始”(读取尚未读取的所有内容),因此由于应用程序出现故障,要读取的数据可能需要几天或几小时。当我的 spark 流应用程序脱机并在几个小时后返回时,或者应用程序第一次运行时,“批处理间隔”是什么?似乎水印与批处理间隔有关。
  • 非常感谢您的解释。在阅读您的答案之前,我没有意识到/理解“过滤器”部分。
  • 批处理间隔的时间例如“22:00:00-22:05:00”是你的应用程序当前运行的'时钟'时间。它与任何数据内容无关。但是,水印中设置的timestamp 取决于内容。但是,如果您的时间戳是在 10 月的某个时间,而您今天在 11 月运行您的应用程序,则不会考虑。
  • 嗨@soMuchToLearn,实际上我想得越多,我就越意识到我不得不承认我对你描述的场景没有完全理解:有很多历史数据排队在 Kafka 中,然后被 Spark 消费和处理。在这种情况下,对消息的处理可能会有所不同,因为 水印 通常对 迟到 生效。老实说,我不确定在这种情况下它的表现如何。也许这是您应该为您的用例尝试的东西。
  • 嗨@mike。 state.setTimeoutTimestamp(state.getCurrentWatermarkMs, "1 hour") 是否也在超时后删除状态?或者只是状态超时,因为我正在尝试使用 if(state.hasTimedOut) 并且它返回 false 并且当检查为 state.option 时,它给出了 None 。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2016-06-25
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多