forked from hamed-abdelhaq/spark-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructuredSparkStreaming.scala
54 lines (36 loc) · 1.26 KB
/
StructuredSparkStreaming.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.hamedabdelhaq.spark.demos.streaming
import org.apache.log4j.varia.NullAppender
import org.apache.log4j.BasicConfigurator
import org.apache.log4j.varia.NullAppender
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.streaming.Trigger
object StructuredSparkStreaming {
def main(args: Array[String]): Unit = {
// val nullAppender = new NullAppender
// BasicConfigurator.configure(nullAppender)
val spark = SparkSession
.builder
.master("local[8]")
.appName("StructuredNetworkWordCount")
.getOrCreate()
spark.conf.set("spark.sql.shuffle.partitions", 2)
spark.sparkContext.setLogLevel("WARN")
import spark.implicits._
val lines = spark.readStream
.format("socket")
.option("host", "localhost")
.option("port", 9999)
.load()
// Split the lines into words
val words = lines.as[String].flatMap(_.split(" "))
// Generate running word count
val wordCounts = words.groupBy("value").count()
// Start running the query that prints the running counts to the console
val query = wordCounts.writeStream
.outputMode("complete")
.format("console")
.trigger(Trigger.ProcessingTime("5 seconds"))
.start()
query.awaitTermination()
}
}