Github user tdas commented on a diff in the pull request:
https://github.com/apache/spark/pull/10385#discussion_r48310515
--- Diff: docs/streaming-programming-guide.md ---
@@ -1415,6 +1415,185 @@ Note that the connections in the pool should be
lazily created on demand and tim
***
+## Accumulator and Broadcast
+
+Accumulator and Broadcast cannot be recovered from checkpoint in Spark
Streaming. If you enable checkpointing and use Accumulator or Broadcast as
well, you'll have to create lazily instantiated singleton instances for
Accumulator and Broadcast so that they can be restarted on driver failures.
This is shown in the following example.
+
+<div class="codetabs">
+<div data-lang="scala" markdown="1">
+{% highlight scala %}
+
+object WordBlacklist {
+
+ @volatile private var instance: Broadcast[Seq[String]] = null
+
+ def getInstance(sc: SparkContext): Broadcast[Seq[String]] = {
+ if (instance == null) {
+ synchronized {
+ if (instance == null) {
+ val wordBlacklist = Seq("a", "b", "c")
+ instance = sc.broadcast(wordBlacklist)
+ }
+ }
+ }
+ instance
+ }
+}
+
+object DroppedWordsCounter {
+
+ @volatile private var instance: Accumulator[Long] = null
+
+ def getInstance(sc: SparkContext): Accumulator[Long] = {
+ if (instance == null) {
+ synchronized {
+ if (instance == null) {
+ instance = sc.accumulator(0L, "WordsInBlacklistCounter")
+ }
+ }
+ }
+ instance
+ }
+}
+
+wordCounts.foreachRDD((rdd: RDD[(String, Int)], time: Time) => {
+ // Get or register the blacklist Broadcast
+ val blacklist = WordBlacklist.getInstance(rdd.sparkContext)
+ // Get or register the droppedWordsCounter Accumulator
+ val droppedWordsCounter =
DroppedWordsCounter.getInstance(rdd.sparkContext)
+ // Use blacklist to drop words and use droppedWordsCounter to count them
+ val counts = rdd.filter { case (word, count) =>
+ if (blacklist.value.contains(word)) {
+ droppedWordsCounter += 1
+ false
+ } else {
+ true
+ }
+ }.collect().mkString("[", ", ", "]")
+ val output = "Counts at time " + time + " " + counts
+ println(output)
--- End diff --
No need to give so much code. remove all the unnecessary cosmetic stuff
like "mkString" and "print" and Files.append ....
Same for all the other code.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]