Github user tdas commented on a diff in the pull request:
https://github.com/apache/spark/pull/2665#discussion_r20122762
--- Diff:
streaming/src/test/scala/org/apache/spark/streaming/BasicOperationsSuite.scala
---
@@ -349,6 +350,43 @@ class BasicOperationsSuite extends TestSuiteBase {
testOperation(inputData, updateStateOperation, outputData, true)
}
+ test("updateStateByKey - with initial value RDD") {
+ val initial = Seq(("a", 1), ("c", 2))
+
+ val inputData =
+ Seq(
+ Seq("a"),
+ Seq("a", "b"),
+ Seq("a", "b", "c"),
+ Seq("a", "b"),
+ Seq("a"),
+ Seq()
+ )
+
+ val outputData =
+ Seq(
+ Seq(("a", 2), ("c", 2)),
+ Seq(("a", 3), ("b", 1), ("c", 2)),
+ Seq(("a", 4), ("b", 2), ("c", 3)),
+ Seq(("a", 5), ("b", 3), ("c", 3)),
+ Seq(("a", 6), ("b", 3), ("c", 3)),
+ Seq(("a", 6), ("b", 3), ("c", 3))
+ )
+
+ val updateStateOperation = (s: DStream[String], initialRDD :
RDD[(String, Int)]) => {
+ val updateFunc = (values: Seq[Int], state: Option[Int]) => {
+ Some(values.sum + state.getOrElse(0))
+ }
+ val newUpdateFunc = (iterator: Iterator[(String, Seq[Int],
Option[Int])]) => {
+ iterator.flatMap(t => updateFunc(t._2, t._3).map(s => (t._1, s)))
+ }
+ s.map(x => (x, 1)).updateStateByKey[Int](newUpdateFunc,
+ new HashPartitioner (numInputPartitions), true, initialRDD)
+ }
+
+ testOperationWithInitial(initial, inputData, updateStateOperation,
outputData, true)
--- End diff --
Instead of setting up and using testOperationWithInitial just for this unit
test, lets try to do the alternative.
```
val updateStateOperation = (s: DStream[String]) => {
val initialRDD = s.context.sparkContext.makeRDD(initial)
val updateFunc = (values: Seq[Int], state: Option[Int]) => {
Some(values.sum + state.getOrElse(0))
}
val newUpdateFunc = (iterator: Iterator[(String, Seq[Int],
Option[Int])]) => {
iterator.flatMap(t => updateFunc(t._2, t._3).map(s => (t._1, s)))
}
s.map(x => (x, 1)).updateStateByKey[Int](newUpdateFunc,
new HashPartitioner (numInputPartitions), true, initialRDD)
}
```
And then use the usual `testOperation`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]