-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12429][Streaming][Doc]Add Accumulator and Broadcast example for Streaming #10385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Java and Python examples
- Loading branch information
commit 9e241e791e5ce2cb0cc3dd7c609339ca8ea11129
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,17 +21,22 @@ | |
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import scala.Tuple2; | ||
| import com.google.common.collect.Lists; | ||
| import com.google.common.io.Files; | ||
|
|
||
| import org.apache.spark.Accumulator; | ||
| import org.apache.spark.SparkConf; | ||
| import org.apache.spark.api.java.JavaPairRDD; | ||
| import org.apache.spark.api.java.JavaSparkContext; | ||
| import org.apache.spark.api.java.function.FlatMapFunction; | ||
| import org.apache.spark.api.java.function.Function; | ||
| import org.apache.spark.api.java.function.Function2; | ||
| import org.apache.spark.api.java.function.PairFunction; | ||
| import org.apache.spark.broadcast.Broadcast; | ||
| import org.apache.spark.streaming.Durations; | ||
| import org.apache.spark.streaming.Time; | ||
| import org.apache.spark.streaming.api.java.JavaDStream; | ||
|
|
@@ -41,7 +46,48 @@ | |
| import org.apache.spark.streaming.api.java.JavaStreamingContextFactory; | ||
|
|
||
| /** | ||
| * Counts words in text encoded with UTF8 received from the network every second. | ||
| * Use this singleton to get or register `Broadcast`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "broadcast variable". |
||
| */ | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Use this singleton to get or register `Accumulator`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment as above for Broadcast.. "an accumulator" |
||
| */ | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Counts words in text encoded with UTF8 received from the network every second. This example also | ||
| * shows how to use lazily instantiated singleton instances for Accumulator and Broadcast so that | ||
| * they can be registered on driver failures. | ||
| * | ||
| * Usage: JavaRecoverableNetworkWordCount <hostname> <port> <checkpoint-directory> <output-file> | ||
| * <hostname> and <port> describe the TCP server that Spark Streaming would connect to receive | ||
|
|
@@ -111,10 +157,27 @@ public Integer call(Integer i1, Integer i2) { | |
| wordCounts.foreachRDD(new Function2<JavaPairRDD<String, Integer>, Time, Void>() { | ||
| @Override | ||
| public Void call(JavaPairRDD<String, Integer> rdd, Time time) throws IOException { | ||
| String counts = "Counts at time " + time + " " + rdd.collect(); | ||
| System.out.println(counts); | ||
| // 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"); | ||
| System.out.println("Appending to " + outputFile.getAbsolutePath()); | ||
| Files.append(counts + "\n", outputFile, Charset.defaultCharset()); | ||
| Files.append(output + "\n", outputFile, Charset.defaultCharset()); | ||
| return null; | ||
| } | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unnecessary stuff.