使用 mapWithState计算单词个数

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
55
56
57
58
59
60
61
62
63
64
65
package allinone

import org.apache.spark.streaming._
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession

/**
* Counts words cumulatively in UTF8 encoded, '\n' delimited text received from the network every
* second starting with initial value of word count.
* Usage: StatefulNetworkWordCount <hostname> <port>
* <hostname> and <port> describe the TCP server that Spark Streaming would connect to receive
* data.
*
* To run this on your local machine, you need to first run a Netcat server
* `$ nc -lk 9999`
* and then run the example
* `spark-submit.sh allinone.StatefulNetworkWordCount localhost 9999`
*/

object StatefulNetworkWordCount {
def main(args: Array[String]) {
if (args.length < 2) {
System.err.println("Usage: StatefulNetworkWordCount <hostname> <port>")
System.exit(1)
}

// 本地模式运行,便于测试
Logger.getLogger("org").setLevel(Level.WARN)
val spark = SparkSession.builder()
.appName(this.getClass.getName)
.master("local[2]")
.getOrCreate()

spark.sparkContext.setLogLevel("WARN")

// Create the context with a 1 second batch size
val ssc = new StreamingContext(spark.sparkContext, Seconds(10))
ssc.checkpoint("/tmp/word-count-map-with-state")

// Initial state RDD for mapWithState operation
val initialRDD = ssc.sparkContext.parallelize(List(("hello", 1), ("world", 1)))

// Create a ReceiverInputDStream on target ip:port and count the
// words in input stream of \n delimited test (eg. generated by 'nc')
val lines = ssc.socketTextStream(args(0), args(1).toInt)
val words = lines.flatMap(_.split(" "))
val wordDstream = words.map(x => (x, 1))

// Update the cumulative count using mapWithState
// This will give a DStream made of state (which is the cumulative count of the words)
// 定义一个接收三个参数的匿名函数
val mappingFunc = (word: String, one: Option[Int], state: State[Int]) => {
val sum = one.getOrElse(0) + state.getOption.getOrElse(0) // 当前值加上上一个批次的该状态的值
val output = (word, sum) // 输出单词和该单词出现的次数
state.update(sum) // 更新当前的状态
output // 该匿名函数的返回值为输出结果
}

// 使用 mapWithState 更新状态
val stateDstream = wordDstream.mapWithState(StateSpec.function(mappingFunc).initialState(initialRDD))
stateDstream.print()
ssc.start()
ssc.awaitTermination()
}
}