8
這是一個用scala編寫的spark spark程序。它每隔1秒計算一次套接字中的字數。結果將是單詞計數,例如,從0到1的單詞計數,然後是從1到2的單詞計數。但是我想知道是否有某種方法可以改變這個程序,以便我們可以累計字數?也就是說,從0到現在爲止的字數。Spark Streaming累計字數
val sparkConf = new SparkConf().setAppName("NetworkWordCount")
val ssc = new StreamingContext(sparkConf, Seconds(1))
// Create a socket stream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
wordCounts.print()
ssc.start()
ssc.awaitTermination()