forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support of initial value for state update.
SPARK-3660 : Initial RDD for updateStateByKey transformation
- Loading branch information
Showing
3 changed files
with
148 additions
and
32 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
.../main/scala/org/apache/spark/examples/streaming/StatefulNetworkWordCountWithInitial.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.examples.streaming | ||
|
||
import org.apache.spark.{HashPartitioner, SparkConf} | ||
import org.apache.spark.streaming._ | ||
import org.apache.spark.streaming.StreamingContext._ | ||
|
||
/** | ||
* Counts words cumulatively in UTF8 encoded, '\n' delimited text received from the network every | ||
* second starting with initial value of word count. | ||
* Usage: StatefulNetworkWordCountWithInitial <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 | ||
* `$ bin/run-example | ||
* org.apache.spark.examples.streaming.StatefulNetworkWordCountWithInitial localhost 9999` | ||
*/ | ||
object StatefulNetworkWordCountWithInitial { | ||
def main(args: Array[String]) { | ||
if (args.length < 2) { | ||
System.err.println("Usage: StatefulNetworkWordCountWithInitial <hostname> <port>") | ||
System.exit(1) | ||
} | ||
|
||
StreamingExamples.setStreamingLogLevels() | ||
|
||
val updateFunc = (values: Seq[Int], state: Option[Int]) => { | ||
val currentCount = values.sum | ||
|
||
val previousCount = state.getOrElse(0) | ||
|
||
Some(currentCount + previousCount) | ||
} | ||
|
||
val newUpdateFunc = (iterator: Iterator[(String, Seq[Int], Option[Int])]) => { | ||
iterator.flatMap(t => updateFunc(t._2, t._3).map(s => (t._1, s))) | ||
} | ||
|
||
val sparkConf = new SparkConf().setAppName("StatefulNetworkWordCountWithInitial") | ||
// Create the context with a 1 second batch size | ||
val ssc = new StreamingContext(sparkConf, Seconds(1)) | ||
ssc.checkpoint(".") | ||
|
||
// Initial RDD input to updateStateByKey | ||
val initialRDD = ssc.sparkContext.parallelize(List(("hello", 1), ("world", 1))) | ||
|
||
// Create a NetworkInputDStream 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 updateStateByKey | ||
// This will give a Dstream made of state (which is the cumulative count of the words) | ||
val stateDstream = wordDstream.updateStateByKey[Int](newUpdateFunc, | ||
new HashPartitioner (ssc.sparkContext.defaultParallelism), true, initialRDD) | ||
stateDstream.print() | ||
ssc.start() | ||
ssc.awaitTermination() | ||
} | ||
} |
This file contains 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 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