aboutsummaryrefslogtreecommitdiff
path: root/docs/streaming-kafka-0-10-integration.md
diff options
context:
space:
mode:
authorcody koeninger <cody@koeninger.org>2016-08-05 10:13:32 +0100
committerSean Owen <sowen@cloudera.com>2016-08-05 10:13:32 +0100
commitc9f2501af278241f780a38b9562e193755ed5af3 (patch)
tree1759cb64fe0647f99610573bf5575ec74fffbe2f /docs/streaming-kafka-0-10-integration.md
parent5effc016c893ce917d535cc1b5026d8e4c846721 (diff)
downloadspark-c9f2501af278241f780a38b9562e193755ed5af3.tar.gz
spark-c9f2501af278241f780a38b9562e193755ed5af3.tar.bz2
spark-c9f2501af278241f780a38b9562e193755ed5af3.zip
[SPARK-16312][STREAMING][KAFKA][DOC] Doc for Kafka 0.10 integration
## What changes were proposed in this pull request? Doc for the Kafka 0.10 integration ## How was this patch tested? Scala code examples were taken from my example repo, so hopefully they compile. Author: cody koeninger <cody@koeninger.org> Closes #14385 from koeninger/SPARK-16312.
Diffstat (limited to 'docs/streaming-kafka-0-10-integration.md')
-rw-r--r--docs/streaming-kafka-0-10-integration.md192
1 files changed, 192 insertions, 0 deletions
diff --git a/docs/streaming-kafka-0-10-integration.md b/docs/streaming-kafka-0-10-integration.md
new file mode 100644
index 0000000000..44c39e3944
--- /dev/null
+++ b/docs/streaming-kafka-0-10-integration.md
@@ -0,0 +1,192 @@
+---
+layout: global
+title: Spark Streaming + Kafka Integration Guide (Kafka broker version 0.10.0 or higher)
+---
+
+The Spark Streaming integration for Kafka 0.10 is similar in design to the 0.8 [Direct Stream approach](streaming-kafka-0-8-integration.html#approach-2-direct-approach-no-receivers). It provides simple parallelism, 1:1 correspondence between Kafka partitions and Spark partitions, and access to offsets and metadata. However, because the newer integration uses the [new Kafka consumer API](http://kafka.apache.org/documentation.html#newconsumerapi) instead of the simple API, there are notable differences in usage. This version of the integration is marked as experimental, so the API is potentially subject to change.
+
+### Linking
+For Scala/Java applications using SBT/Maven project definitions, link your streaming application with the following artifact (see [Linking section](streaming-programming-guide.html#linking) in the main programming guide for further information).
+
+ groupId = org.apache.spark
+ artifactId = spark-streaming-kafka-0-10_{{site.SCALA_BINARY_VERSION}}
+ version = {{site.SPARK_VERSION_SHORT}}
+
+### Creating a Direct Stream
+ Note that the namespace for the import includes the version, org.apache.spark.streaming.kafka010
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ import org.apache.kafka.clients.consumer.ConsumerRecord
+ import org.apache.kafka.common.serialization.StringDeserializer
+ import org.apache.spark.streaming.kafka010._
+ import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent
+ import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
+
+ val kafkaParams = Map[String, Object](
+ "bootstrap.servers" -> "localhost:9092,anotherhost:9092",
+ "key.deserializer" -> classOf[StringDeserializer],
+ "value.deserializer" -> classOf[StringDeserializer],
+ "group.id" -> "example",
+ "auto.offset.reset" -> "latest",
+ "enable.auto.commit" -> (false: java.lang.Boolean)
+ )
+
+ val topics = Array("topicA", "topicB")
+ val stream = KafkaUtils.createDirectStream[String, String](
+ streamingContext,
+ PreferConsistent,
+ Subscribe[String, String](topics, kafkaParams)
+ )
+
+ stream.map(record => (record.key, record.value))
+
+Each item in the stream is a [ConsumerRecord](http://kafka.apache.org/0100/javadoc/org/apache/kafka/clients/consumer/ConsumerRecord.html)
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+For possible kafkaParams, see [Kafka consumer config docs](http://kafka.apache.org/documentation.html#newconsumerconfigs).
+Note that enable.auto.commit is disabled, for discussion see [Storing Offsets](streaming-kafka-0-10-integration.html#storing-offsets) below.
+
+### LocationStrategies
+The new Kafka consumer API will pre-fetch messages into buffers. Therefore it is important for performance reasons that the Spark integration keep cached consumers on executors (rather than recreating them for each batch), and prefer to schedule partitions on the host locations that have the appropriate consumers.
+
+In most cases, you should use `LocationStrategies.PreferConsistent` as shown above. This will distribute partitions evenly across available executors. If your executors are on the same hosts as your Kafka brokers, use `PreferBrokers`, which will prefer to schedule partitions on the Kafka leader for that partition. Finally, if you have a significant skew in load among partitions, use `PreferFixed`. This allows you to specify an explicit mapping of partitions to hosts (any unspecified partitions will use a consistent location).
+
+The cache for consumers has a default maximum size of 64. If you expect to be handling more than (64 * number of executors) Kafka partitions, you can change this setting via `spark.streaming.kafka.consumer.cache.maxCapacity`
+
+### ConsumerStrategies
+The new Kafka consumer API has a number of different ways to specify topics, some of which require considerable post-object-instantiation setup. `ConsumerStrategies` provides an abstraction that allows Spark to obtain properly configured consumers even after restart from checkpoint.
+
+`ConsumerStrategies.Subscribe`, as shown above, allows you to subscribe to a fixed collection of topics. `SubscribePattern` allows you to use a regex to specify topics of interest. Note that unlike the 0.8 integration, using `Subscribe` or `SubscribePattern` should respond to adding partitions during a running stream. Finally, `Assign` allows you to specify a fixed collection of partitions. All three strategies have overloaded constructors that allow you to specify the starting offset for a particular partition.
+
+If you have specific consumer setup needs that are not met by the options above, `ConsumerStrategy` is a public class that you can extend.
+
+### Creating an RDD
+If you have a use case that is better suited to batch processing, you can create an RDD for a defined range of offsets.
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ // Import dependencies and create kafka params as in Create Direct Stream above
+
+ val offsetRanges = Array(
+ // topic, partition, inclusive starting offset, exclusive ending offset
+ OffsetRange("test", 0, 0, 100),
+ OffsetRange("test", 1, 0, 100)
+ )
+
+ val rdd = KafkaUtils.createRDD[String, String](sparkContext, kafkaParams, offsetRanges, PreferConsistent)
+
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+Note that you cannot use `PreferBrokers`, because without the stream there is not a driver-side consumer to automatically look up broker metadata for you. Use `PreferFixed` with your own metadata lookups if necessary.
+
+### Obtaining Offsets
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ stream.foreachRDD { rdd =>
+ val offsetRanges = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
+ rdd.foreachPartition { iter =>
+ val o: OffsetRange = offsetRanges(TaskContext.get.partitionId)
+ println(s"${o.topic} ${o.partition} ${o.fromOffset} ${o.untilOffset}")
+ }
+ }
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+Note that the typecast to `HasOffsetRanges` will only succeed if it is done in the first method called on the result of `createDirectStream`, not later down a chain of methods. Be aware that the one-to-one mapping between RDD partition and Kafka partition does not remain after any methods that shuffle or repartition, e.g. reduceByKey() or window().
+
+### Storing Offsets
+Kafka delivery semantics in the case of failure depend on how and when offsets are stored. Spark output operations are [at-least-once](streaming-programming-guide.html#semantics-of-output-operations). So if you want the equivalent of exactly-once semantics, you must either store offsets after an idempotent output, or store offsets in an atomic transaction alongside output. With this integration, you have 3 options, in order of increasing reliablity (and code complexity), for how to store offsets.
+
+#### Checkpoints
+If you enable Spark [checkpointing](streaming-programming-guide.html#checkpointing), offsets will be stored in the checkpoint. This is easy to enable, but there are drawbacks. Your output operation must be idempotent, since you will get repeated outputs; transactions are not an option. Furthermore, you cannot recover from a checkpoint if your application code has changed. For planned upgrades, you can mitigate this by running the new code at the same time as the old code (since outputs need to be idempotent anyway, they should not clash). But for unplanned failures that require code changes, you will lose data unless you have another way to identify known good starting offsets.
+
+#### Kafka itself
+Kafka has an offset commit API that stores offsets in a special Kafka topic. By default, the new consumer will periodically auto-commit offsets. This is almost certainly not what you want, because messages successfully polled by the consumer may not yet have resulted in a Spark output operation, resulting in undefined semantics. This is why the stream example above sets "enable.auto.commit" to false. However, you can commit offsets to Kafka after you know your output has been stored, using the `commitAsync` API. The benefit as compared to checkpoints is that Kafka is a durable store regardless of changes to your application code. However, Kafka is not transactional, so your outputs must still be idempotent.
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ stream.foreachRDD { rdd =>
+ val offsets = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
+
+ // some time later, after outputs have completed
+ stream.asInstanceOf[CanCommitOffsets].commitAsync(offsets)
+ }
+
+As with HasOffsetRanges, the cast to CanCommitOffsets will only succeed if called on the result of createDirectStream, not after transformations. The commitAsync call is threadsafe, but must occur after outputs if you want meaningful semantics.
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+#### Your own data store
+For data stores that support transactions, saving offsets in the same transaction as the results can keep the two in sync, even in failure situations. If you're careful about detecting repeated or skipped offset ranges, rolling back the transaction prevents duplicated or lost messages from affecting results. This gives the equivalent of exactly-once semantics. It is also possible to use this tactic even for outputs that result from aggregations, which are typically hard to make idempotent.
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ // The details depend on your data store, but the general idea looks like this
+
+ // begin from the the offsets committed to the database
+ val fromOffsets = selectOffsetsFromYourDatabase.map { resultSet =>
+ new TopicPartition(resultSet.string("topic")), resultSet.int("partition")) -> resultSet.long("offset")
+ }.toMap
+
+ val stream = KafkaUtils.createDirectStream[String, String](
+ streamingContext,
+ PreferConsistent,
+ Assign[String, String](fromOffsets.keys.toList, kafkaParams, fromOffsets)
+ )
+
+ stream.foreachRDD { rdd =>
+ val offsetRanges = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
+
+ val results = yourCalculation(rdd)
+
+ yourTransactionBlock {
+ // update results
+
+ // update offsets where the end of existing offsets matches the beginning of this batch of offsets
+
+ // assert that offsets were updated correctly
+ }
+ }
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+### SSL / TLS
+The new Kafka consumer [supports SSL](http://kafka.apache.org/documentation.html#security_ssl). To enable it, set kafkaParams appropriately before passing to `createDirectStream` / `createRDD`. Note that this only applies to communication between Spark and Kafka brokers; you are still responsible for separately [securing](security.html) Spark inter-node communication.
+
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+ val kafkaParams = Map[String, Object](
+ // the usual params, make sure to change the port in bootstrap.servers if 9092 is not TLS
+ "security.protocol" -> "SSL",
+ "ssl.truststore.location" -> "/some-directory/kafka.client.truststore.jks",
+ "ssl.truststore.password" -> "test1234",
+ "ssl.keystore.location" -> "/some-directory/kafka.client.keystore.jks",
+ "ssl.keystore.password" -> "test1234",
+ "ssl.key.password" -> "test1234"
+ )
+</div>
+<div data-lang="java" markdown="1">
+</div>
+</div>
+
+### Deploying
+
+As with any Spark applications, `spark-submit` is used to launch your application.
+
+For Scala and Java applications, if you are using SBT or Maven for project management, then package `spark-streaming-kafka-0-10_{{site.SCALA_BINARY_VERSION}}` and its dependencies into the application JAR. Make sure `spark-core_{{site.SCALA_BINARY_VERSION}}` and `spark-streaming_{{site.SCALA_BINARY_VERSION}}` are marked as `provided` dependencies as those are already present in a Spark installation. Then use `spark-submit` to launch your application (see [Deploying section](streaming-programming-guide.html#deploying-applications) in the main programming guide).
+