spark-streaming Demo
Add Dependency
<dependency>
<groupId>org.apache.spark</groupId>
<artifactid>spark-core_2.10 </artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactid>spark-streaming_2.10</artifactid >
<version>2.0.2</version>
</dependency>
Note: The above version of debug passes, other versions may have jar package dependency conflict issues. Code
Import Java.util.Arrays;
Import Java.util.Iterator;
Import Java.util.regex.Pattern;
Import org.apache.spark.api.java.function.FlatMapFunction;
Import org.apache.spark.streaming.Duration; Import Scala.
Tuple2;
Import org.apache.spark.SparkConf;
Import Org.apache.spark.api.java.function.Function2;
Import org.apache.spark.api.java.function.PairFunction;
Import Org.apache.spark.api.java.StorageLevels;
Import org.apache.spark.streaming.Durations;
Import Org.apache.spark.streaming.api.java.JavaDStream;
Import Org.apache.spark.streaming.api.java.JavaPairDStream;
Import Org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
Import Org.apache.spark.streaming.api.java.JavaStreamingContext;
public class Javanetworkwordcount {private static final Pattern SPACE = Pattern.compile (""); public static void Main (string[] args) throws Exception {if (Args.length < 2) {System.err.println ("Usage:
Javanetworkwordcount
specify IP and port as arguments to the main function
localhost 1111
Note: In particular, it is important to be aware that the VM options must be specified as-dspark.master=local, otherwise the spark task will not start normally and the maser URL error is not specified. NC boot port and send data
Enter the NC LK 1111 in the console and enter a series of text returns to see the number of individual words we have counted.
As shown in the following illustration:
attached to the official code
Note: The official code is problematic and needs to be adjusted
https://github.com/apache/spark/blob/v2.0.2/examples/src/main/java/org/apache/spark/examples/streaming/ Javanetworkwordcount.java