title: Reading from the beginning of a Kafka Topic using Structured Streaming when query started
body: <p>im using structured streaming to read from the kafka topic, using <code>spark 2.4</code> and <code>scala 2.12</code></p>
  <p>im using a checkpoint to make my query fault-tolerant.</p>
  <p>however everytime i start the query it jumps to the current offset without reading the exisitng data before it connected onto the topic.</p>
  <p>is there a config for the kafka stream im missing?</p>
  <p>READ:</p>
  <pre><code> val df = spark.readStream
      .format(&quot;kafka&quot;)
      .option(&quot;kafka.bootstrap.servers&quot;, &quot;localhost:9092&quot;)
      .option(&quot;subscribe&quot;, &quot;test&quot;)
      .option(&quot;maxOffsetsPerTrigger&quot;,&quot;1&quot;)
      .option(&quot;startingOffset&quot;,&quot;earliest&quot;)
      .option(&quot;auto.offset.reset&quot;,&quot;earliest&quot;)
      .load()
  val msg = df.select($&quot;value&quot; cast &quot;string&quot;, $&quot;topic&quot;, $&quot;partition&quot;, $&quot;offset&quot;)
  </code></pre>
  <p>Write:</p>
  <pre><code>val query= msg.writeStream
      .foreachBatch(
        (dfbatch: Dataset[Row], batchid: Long) =&gt;
        {
        println(s&quot;IM AT BATCH ID: $batchid&quot;)
        dfbatch.show()
        dfbatch.write.csv(s&quot;s3a://abucket/$param&quot;)
        }
      )
        .option(&quot;checkpointLocation&quot;,&quot;s3a://checkpoint/&quot;)
        .trigger(Trigger.ProcessingTime(&quot;10 seconds&quot;))
        .format(&quot;console&quot;)
        .start()
  
   query.awaitTermination()
  </code></pre>
  <p>EDIT:</p>
  <p>here are the logs after i emptied my checkpoint dir:</p>
  <pre><code>0/07/11 18:15:16 INFO CheckpointFileManager: Writing atomically to s3a://checkpoint/metadata using temp file s3a://checkpoint/.metadata.304a751a-68b7-4b8d-858c-3aa5df272db4.tmp
  20/07/11 18:15:17 INFO CheckpointFileManager: Renamed temp file s3a://checkpoint/.metadata.304a751a-68b7-4b8d-858c-3aa5df272db4.tmp to s3a://checkpoint/metadata
  20/07/11 18:15:17 INFO MicroBatchExecution: Starting [id = e83c6066-9611-4e9b-97d5-d02421b2d1d6, runId = e77896e3-ce76-488b-8345-7a29cc0d7d0b]. Use s3a://checkpoint/ to store the query checkpoint.
  20/07/11 18:15:17 INFO MicroBatchExecution: Using MicroBatchReader [KafkaV2[Subscribe[test]]] from DataSourceV2 named 'kafka' [org.apache.spark.sql.kafka010.KafkaSourceProvider@2375c472]
  20/07/11 18:15:17 INFO MicroBatchExecution: Starting new streaming query.
  20/07/11 18:15:17 INFO MicroBatchExecution: Stream started from {}
  20/07/11 18:15:18 INFO ConsumerConfig: ConsumerConfig values: 
      auto.commit.interval.ms = 5000
      auto.offset.reset = earliest
      bootstrap.servers = [localhost:9092]
      check.crcs = true
      client.id = 
      connections.max.idle.ms = 540000
      default.api.timeout.ms = 60000
      enable.auto.commit = false
      exclude.internal.topics = true
      fetch.max.bytes = 52428800
      fetch.max.wait.ms = 500
      fetch.min.bytes = 1
      group.id = spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0
      heartbeat.interval.ms = 3000
      interceptor.classes = []
      internal.leave.group.on.close = true
      isolation.level = read_uncommitted
      key.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer
      max.partition.fetch.bytes = 1048576
      max.poll.interval.ms = 300000
      max.poll.records = 1
      metadata.max.age.ms = 300000
      metric.reporters = []
      metrics.num.samples = 2
      metrics.recording.level = INFO
      metrics.sample.window.ms = 30000
      partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
      receive.buffer.bytes = 65536
      reconnect.backoff.max.ms = 1000
      reconnect.backoff.ms = 50
      request.timeout.ms = 30000
      retry.backoff.ms = 100
      sasl.client.callback.handler.class = null
      sasl.jaas.config = null
      sasl.kerberos.kinit.cmd = /usr/bin/kinit
      sasl.kerberos.min.time.before.relogin = 60000
      sasl.kerberos.service.name = null
      sasl.kerberos.ticket.renew.jitter = 0.05
      sasl.kerberos.ticket.renew.window.factor = 0.8
      sasl.login.callback.handler.class = null
      sasl.login.class = null
      sasl.login.refresh.buffer.seconds = 300
      sasl.login.refresh.min.period.seconds = 60
      sasl.login.refresh.window.factor = 0.8
      sasl.login.refresh.window.jitter = 0.05
      sasl.mechanism = GSSAPI
      security.protocol = PLAINTEXT
      send.buffer.bytes = 131072
      session.timeout.ms = 10000
      ssl.cipher.suites = null
      ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
      ssl.endpoint.identification.algorithm = https
      ssl.key.password = null
      ssl.keymanager.algorithm = SunX509
      ssl.keystore.location = null
      ssl.keystore.password = null
      ssl.keystore.type = JKS
      ssl.protocol = TLS
      ssl.provider = null
      ssl.secure.random.implementation = null
      ssl.trustmanager.algorithm = PKIX
      ssl.truststore.location = null
      ssl.truststore.password = null
      ssl.truststore.type = JKS
      value.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer
  
  20/07/11 18:15:18 INFO AppInfoParser: Kafka version : 2.0.0
  20/07/11 18:15:18 INFO AppInfoParser: Kafka commitId : 3402a8361b734732
  20/07/11 18:15:18 INFO Metadata: Cluster ID: X8K8aVFyRi6OcUDs1zXOhQ
  20/07/11 18:15:18 INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Discovered group coordinator Myuser-PC:9092 (id: 2147483647 rack: null)
  20/07/11 18:15:18 INFO ConsumerCoordinator: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Revoking previously assigned partitions []
  20/07/11 18:15:18 INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] (Re-)joining group
  20/07/11 18:15:18 INFO AbstractCoordinator: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Successfully joined group with generation 1
  20/07/11 18:15:18 INFO ConsumerCoordinator: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Setting newly assigned partitions [test-0]
  20/07/11 18:15:18 INFO Fetcher: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Resetting offset for partition test-0 to offset 0.
  20/07/11 18:15:18 INFO Fetcher: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Resetting offset for partition test-0 to offset 18.
  20/07/11 18:15:18 INFO deprecation: io.bytes.per.checksum is deprecated. Instead, use dfs.bytes-per-checksum
  20/07/11 18:15:18 INFO CheckpointFileManager: Writing atomically to s3a://checkpoint/sources/0/0 using temp file s3a://checkpoint/sources/0/.0.e900c0dd-dbb5-4ca7-ada2-e3a84e892c5f.tmp
  20/07/11 18:15:19 INFO CheckpointFileManager: Renamed temp file s3a://checkpoint/sources/0/.0.e900c0dd-dbb5-4ca7-ada2-e3a84e892c5f.tmp to s3a://checkpoint/sources/0/0
  20/07/11 18:15:19 INFO KafkaMicroBatchReader: Initial offsets: {&quot;test&quot;:{&quot;0&quot;:18}}
  20/07/11 18:15:19 INFO Fetcher: [Consumer clientId=consumer-1, groupId=spark-kafka-source-1fa35d7f-b356-4806-9ee7-658ef48c837d--2088528104-driver-0] Resetting offset for partition test-0 to offset 18.
  20/07/11 18:15:19 INFO CheckpointFileManager: Writing atomically to s3a://checkpoint/offsets/0 using temp file s3a://checkpoint/offsets/.0.a7a4e7f3-7e4a-433f-8532-23d6179c3b98.tmp
  20/07/11 18:15:19 INFO CheckpointFileManager: Renamed temp file s3a://checkpoint/offsets/.0.a7a4e7f3-7e4a-433f-8532-23d6179c3b98.tmp to s3a://checkpoint/offsets/0
  20/07/11 18:15:19 INFO MicroBatchExecution: Committed offsets for batch 0. Metadata OffsetSeqMetadata(0,1594480519101,Map(spark.sql.streaming.stateStore.providerClass -&gt; org.apache.spark.sql.execution.streaming.state.HDFSBackedStateStoreProvider, spark.sql.streaming.flatMapGroupsWithState.stateFormatVersion -&gt; 2, spark.sql.streaming.multipleWatermarkPolicy -&gt; min, spark.sql.streaming.aggregation.stateFormatVersion -&gt; 2, spark.sql.shuffle.partitions -&gt; 200))
  20/07/11 18:15:20 INFO KafkaMicroBatchReader: Partitions added: Map()
  20/07/11 18:15:20 INFO CodeGenerator: Code generated in 171.85213 ms
  20/07/11 18:15:20 INFO CodeGenerator: Code generated in 23.189288 ms
  </code></pre>
  <p>i went inside the checkpoint dir and modified the data and it started processing from the beginning!
  there must be a config that does the same..</p>