Skip to content

Commit 447b2b5

Browse files
etcondietdas
authored andcommitted
[SPARK-19584][SS][DOCS] update structured streaming documentation around batch mode
## What changes were proposed in this pull request? Revision to structured-streaming-kafka-integration.md to reflect new Batch query specification and options. zsxwing tdas Please review http://spark.apache.org/contributing.html before opening a pull request. Author: Tyson Condie <tcondie@gmail.com> Closes #16918 from tcondie/kafka-docs.
1 parent f48c5a5 commit 447b2b5

File tree

1 file changed

+149
-11
lines changed

1 file changed

+149
-11
lines changed

docs/structured-streaming-kafka-integration.md

Lines changed: 149 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,124 @@ ds3.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
119119
</div>
120120
</div>
121121

122+
### Creating a Kafka Source Batch
123+
If you have a use case that is better suited to batch processing,
124+
you can create an Dataset/DataFrame for a defined range of offsets.
125+
126+
<div class="codetabs">
127+
<div data-lang="scala" markdown="1">
128+
{% highlight scala %}
129+
130+
// Subscribe to 1 topic defaults to the earliest and latest offsets
131+
val ds1 = spark
132+
.read
133+
.format("kafka")
134+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
135+
.option("subscribe", "topic1")
136+
.load()
137+
ds1.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
138+
.as[(String, String)]
139+
140+
// Subscribe to multiple topics, specifying explicit Kafka offsets
141+
val ds2 = spark
142+
.read
143+
.format("kafka")
144+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
145+
.option("subscribe", "topic1,topic2")
146+
.option("startingOffsets", """{"topic1":{"0":23,"1":-2},"topic2":{"0":-2}}""")
147+
.option("endingOffsets", """{"topic1":{"0":50,"1":-1},"topic2":{"0":-1}}""")
148+
.load()
149+
ds2.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
150+
.as[(String, String)]
151+
152+
// Subscribe to a pattern, at the earliest and latest offsets
153+
val ds3 = spark
154+
.read
155+
.format("kafka")
156+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
157+
.option("subscribePattern", "topic.*")
158+
.option("startingOffsets", "earliest")
159+
.option("endingOffsets", "latest")
160+
.load()
161+
ds3.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
162+
.as[(String, String)]
163+
164+
{% endhighlight %}
165+
</div>
166+
<div data-lang="java" markdown="1">
167+
{% highlight java %}
168+
169+
// Subscribe to 1 topic defaults to the earliest and latest offsets
170+
Dataset<Row> ds1 = spark
171+
.read()
172+
.format("kafka")
173+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
174+
.option("subscribe", "topic1")
175+
.load();
176+
ds1.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)");
177+
178+
// Subscribe to multiple topics, specifying explicit Kafka offsets
179+
Dataset<Row> ds2 = spark
180+
.read()
181+
.format("kafka")
182+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
183+
.option("subscribe", "topic1,topic2")
184+
.option("startingOffsets", "{\"topic1\":{\"0\":23,\"1\":-2},\"topic2\":{\"0\":-2}}")
185+
.option("endingOffsets", "{\"topic1\":{\"0\":50,\"1\":-1},\"topic2\":{\"0\":-1}}")
186+
.load();
187+
ds2.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)");
188+
189+
// Subscribe to a pattern, at the earliest and latest offsets
190+
Dataset<Row> ds3 = spark
191+
.read()
192+
.format("kafka")
193+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
194+
.option("subscribePattern", "topic.*")
195+
.option("startingOffsets", "earliest")
196+
.option("endingOffsets", "latest")
197+
.load();
198+
ds3.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)");
199+
200+
{% endhighlight %}
201+
</div>
202+
<div data-lang="python" markdown="1">
203+
{% highlight python %}
204+
205+
# Subscribe to 1 topic defaults to the earliest and latest offsets
206+
ds1 = spark \
207+
.read \
208+
.format("kafka") \
209+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2") \
210+
.option("subscribe", "topic1") \
211+
.load()
212+
ds1.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
213+
214+
# Subscribe to multiple topics, specifying explicit Kafka offsets
215+
ds2 = spark \
216+
.read \
217+
.format("kafka") \
218+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2") \
219+
.option("subscribe", "topic1,topic2") \
220+
.option("startingOffsets", """{"topic1":{"0":23,"1":-2},"topic2":{"0":-2}}""") \
221+
.option("endingOffsets", """{"topic1":{"0":50,"1":-1},"topic2":{"0":-1}}""") \
222+
.load()
223+
ds2.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
224+
225+
# Subscribe to a pattern, at the earliest and latest offsets
226+
ds3 = spark \
227+
.read \
228+
.format("kafka") \
229+
.option("kafka.bootstrap.servers", "host1:port1,host2:port2") \
230+
.option("subscribePattern", "topic.*") \
231+
.option("startingOffsets", "earliest") \
232+
.option("endingOffsets", "latest") \
233+
.load()
234+
ds3.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
235+
236+
{% endhighlight %}
237+
</div>
238+
</div>
239+
122240
Each row in the source has the following schema:
123241
<table class="table">
124242
<tr><th>Column</th><th>Type</th></tr>
@@ -152,7 +270,8 @@ Each row in the source has the following schema:
152270
</tr>
153271
</table>
154272

155-
The following options must be set for the Kafka source.
273+
The following options must be set for the Kafka source
274+
for both batch and streaming queries.
156275

157276
<table class="table">
158277
<tr><th>Option</th><th>value</th><th>meaning</th></tr>
@@ -187,50 +306,69 @@ The following options must be set for the Kafka source.
187306
The following configurations are optional:
188307

189308
<table class="table">
190-
<tr><th>Option</th><th>value</th><th>default</th><th>meaning</th></tr>
309+
<tr><th>Option</th><th>value</th><th>default</th><th>query type</th><th>meaning</th></tr>
191310
<tr>
192311
<td>startingOffsets</td>
193-
<td>earliest, latest, or json string
194-
{"topicA":{"0":23,"1":-1},"topicB":{"0":-2}}
312+
<td>"earliest", "latest" (streaming only), or json string
313+
""" {"topicA":{"0":23,"1":-1},"topicB":{"0":-2}} """
195314
</td>
196-
<td>latest</td>
315+
<td>"latest" for streaming, "earliest" for batch</td>
316+
<td>streaming and batch</td>
197317
<td>The start point when a query is started, either "earliest" which is from the earliest offsets,
198318
"latest" which is just from the latest offsets, or a json string specifying a starting offset for
199319
each TopicPartition. In the json, -2 as an offset can be used to refer to earliest, -1 to latest.
200-
Note: This only applies when a new Streaming query is started, and that resuming will always pick
201-
up from where the query left off. Newly discovered partitions during a query will start at
320+
Note: For batch queries, latest (either implicitly or by using -1 in json) is not allowed.
321+
For streaming queries, this only applies when a new query is started, and that resuming will
322+
always pick up from where the query left off. Newly discovered partitions during a query will start at
202323
earliest.</td>
203324
</tr>
325+
<tr>
326+
<td>endingOffsets</td>
327+
<td>latest or json string
328+
{"topicA":{"0":23,"1":-1},"topicB":{"0":-1}}
329+
</td>
330+
<td>latest</td>
331+
<td>batch query</td>
332+
<td>The end point when a batch query is ended, either "latest" which is just referred to the
333+
latest, or a json string specifying an ending offset for each TopicPartition. In the json, -1
334+
as an offset can be used to refer to latest, and -2 (earliest) as an offset is not allowed.</td>
335+
</tr>
204336
<tr>
205337
<td>failOnDataLoss</td>
206338
<td>true or false</td>
207339
<td>true</td>
208-
<td>Whether to fail the query when it's possible that data is lost (e.g., topics are deleted, or
340+
<td>streaming query</td>
341+
<td>Whether to fail the query when it's possible that data is lost (e.g., topics are deleted, or
209342
offsets are out of range). This may be a false alarm. You can disable it when it doesn't work
210-
as you expected.</td>
343+
as you expected. Batch queries will always fail if it fails to read any data from the provided
344+
offsets due to lost data.</td>
211345
</tr>
212346
<tr>
213347
<td>kafkaConsumer.pollTimeoutMs</td>
214348
<td>long</td>
215349
<td>512</td>
350+
<td>streaming and batch</td>
216351
<td>The timeout in milliseconds to poll data from Kafka in executors.</td>
217352
</tr>
218353
<tr>
219354
<td>fetchOffset.numRetries</td>
220355
<td>int</td>
221356
<td>3</td>
222-
<td>Number of times to retry before giving up fatch Kafka latest offsets.</td>
357+
<td>streaming and batch</td>
358+
<td>Number of times to retry before giving up fetching Kafka offsets.</td>
223359
</tr>
224360
<tr>
225361
<td>fetchOffset.retryIntervalMs</td>
226362
<td>long</td>
227363
<td>10</td>
364+
<td>streaming and batch</td>
228365
<td>milliseconds to wait before retrying to fetch Kafka offsets</td>
229366
</tr>
230367
<tr>
231368
<td>maxOffsetsPerTrigger</td>
232369
<td>long</td>
233370
<td>none</td>
371+
<td>streaming and batch</td>
234372
<td>Rate limit on maximum number of offsets processed per trigger interval. The specified total number of offsets will be proportionally split across topicPartitions of different volume.</td>
235373
</tr>
236374
</table>
@@ -246,7 +384,7 @@ Note that the following Kafka params cannot be set and the Kafka source will thr
246384
where to start instead. Structured Streaming manages which offsets are consumed internally, rather
247385
than rely on the kafka Consumer to do it. This will ensure that no data is missed when new
248386
topics/partitions are dynamically subscribed. Note that `startingOffsets` only applies when a new
249-
Streaming query is started, and that resuming will always pick up from where the query left off.
387+
streaming query is started, and that resuming will always pick up from where the query left off.
250388
- **key.deserializer**: Keys are always deserialized as byte arrays with ByteArrayDeserializer. Use
251389
DataFrame operations to explicitly deserialize the keys.
252390
- **value.deserializer**: Values are always deserialized as byte arrays with ByteArrayDeserializer.

0 commit comments

Comments
 (0)