Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10385#discussion_r48310590
  
    --- 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)
    +  println("Dropped " + droppedWordsCounter.value + " word(s) totally")
    +  println("Appending to " + outputFile.getAbsolutePath)
    +  Files.append(output + "\n", outputFile, Charset.defaultCharset())
    +})
    +
    +{% endhighlight %}
    +
    +See the full [source 
code]({{site.SPARK_GITHUB_URL}}/blob/master/examples/src/main/scala/org/apache/spark/examples/streaming/RecoverableNetworkWordCount.scala).
    +</div>
    +<div data-lang="java" markdown="1">
    +{% highlight java %}
    +
    +class JavaWordBlacklist {
    +
    +  private static volatile Broadcast<List<String>> instance = null;
    +
    +  public static Broadcast<List<String>> getInstance(JavaSparkContext jsc) {
    +    if (instance == null) {
    +      synchronized (WordBlacklist.class) {
    +        if (instance == null) {
    +          List<String> wordBlacklist = Arrays.asList("a", "b", "c");
    +          instance = jsc.broadcast(wordBlacklist);
    +        }
    +      }
    +    }
    +    return instance;
    +  }
    +}
    +
    +class JavaDroppedWordsCounter {
    +
    +  private static volatile Accumulator<Integer> instance = null;
    +
    +  public static Accumulator<Integer> getInstance(JavaSparkContext jsc) {
    +    if (instance == null) {
    +      synchronized (DroppedWordsCounter.class) {
    +        if (instance == null) {
    +          instance = jsc.accumulator(0, "WordsInBlacklistCounter");
    +        }
    +      }
    +    }
    +    return instance;
    +  }
    +}
    +
    +wordCounts.foreachRDD(new Function2<JavaPairRDD<String, Integer>, Time, 
Void>() {
    +  @Override
    +  public Void call(JavaPairRDD<String, Integer> rdd, Time time) throws 
IOException {
    +    // Get or register the blacklist Broadcast
    +    final Broadcast<List<String>> blacklist = 
JavaWordBlacklist.getInstance(new JavaSparkContext(rdd.context()));
    +    // Get or register the droppedWordsCounter Accumulator
    +    final Accumulator<Integer> droppedWordsCounter = 
JavaDroppedWordsCounter.getInstance(new JavaSparkContext(rdd.context()));
    +    // Use blacklist to drop words and use droppedWordsCounter to count 
them
    +    String counts = rdd.filter(new Function<Tuple2<String, Integer>, 
Boolean>() {
    +      @Override
    +      public Boolean call(Tuple2<String, Integer> wordCount) throws 
Exception {
    +        if (blacklist.value().contains(wordCount._1())) {
    +          droppedWordsCounter.add(wordCount._2());
    +          return false;
    +        } else {
    +          return true;
    +        }
    +      }
    +    }).collect().toString();
    +    String output = "Counts at time " + time + " " + counts;
    +    System.out.println(output);
    +    System.out.println("Dropped " + droppedWordsCounter.value() + " 
word(s) totally");
    --- End diff --
    
    remove unnecessary stuff.


---
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]

Reply via email to