来自Kafka检查点和确认的Spark结构化流传输

时间:2019-04-24 05:58:25

标签: apache-spark apache-kafka spark-structured-streaming

在我的Spark结构化流应用程序中,我正在从Kafka中读取消息,对其进行过滤,然后最终保留到Cassandra。我正在使用spark 2.4.1。来自结构化流媒体文档

  

容错语义   提供端到端的一次语义是结构化流设计背后的主要目标之一。为此,我们设计了结构化流源,接收器和执行引擎,以可靠地跟踪处理的确切进度,以便它可以通过重新启动和/或重新处理来处理任何类型的故障。假定每个流源都有偏移量(类似于Kafka偏移量或Kinesis序列号),以跟踪流中的读取位置。引擎使用检查点和预写日志来记录每个触发器中正在处理的数据的偏移范围。流接收器被设计为是幂等的,用于处理后处理。结合使用可重播的源和幂等的接收器,结构化流可以确保在发生任何故障时端到端的一次精确语义。

但是我不确定Spark如何真正实现这一目标。在我的情况下,如果Cassandra集群宕机导致写入操作失败,那么Kafka的检查点将不会记录这些偏移量。

Kafka检查点偏移是仅基于对Kafka的成功读取,还是针对每条消息都考虑了包括写入在内的整个操作?

2 个答案:

答案 0 :(得分:0)

火花结构化流不像“普通” kafka消费者那样向kafka提交偏移量。 Spark正在内部使用检查点机制来管理偏移量。

看看以下问题的第一个回答,它很好地说明了如何使用检查点和commitslog管理状态:How to get Kafka offsets for structured query for manual and reliable offset management?

答案 1 :(得分:0)

Spark使用多个日志文件来确保容错能力。 与查询相关的是偏移量日志和提交日志。 来自StreamExecution类文档:

'

因此,当它从Kafka读取时,会将偏移量写入 /** * A write-ahead-log that records the offsets that are present in each batch. In order to ensure * that a given batch will always consist of the same data, we write to this log *before* any * processing is done. Thus, the Nth record in this log indicated data that is currently being * processed and the N-1th entry indicates which offsets have been durably committed to the sink. */ val offsetLog = new OffsetSeqLog(sparkSession, checkpointFile("offsets")) /** * A log that records the batch ids that have completed. This is used to check if a batch was * fully processed, and its output was committed to the sink, hence no need to process it again. * This is used (for instance) during restart, to help identify which batch to run next. */ val commitLog = new CommitLog(sparkSession, checkpointFile("commits")) ,只有在处理完数据并将写入到接收器(在您的情况下为Cassandra)中,才会将偏移量写入offsetLog

相关问题