Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11122#discussion_r52401975
  
    --- Diff: 
streaming/src/test/scala/org/apache/spark/streaming/BasicOperationsSuite.scala 
---
    @@ -467,6 +467,73 @@ class BasicOperationsSuite extends TestSuiteBase {
         testOperation(inputData, updateStateOperation, outputData, true)
       }
     
    +  test("updateStateByKey - testing time stamps as input") {
    +    type StreamingState = Long
    +    val initial: Seq[(String, StreamingState)] = Seq(("a", 0L), ("c", 0L))
    +
    +    val inputData =
    +      Seq(
    +        Seq("a"),
    +        Seq("a", "b"),
    +        Seq("a", "b", "c"),
    +        Seq("a", "b"),
    +        Seq("a"),
    +        Seq()
    +      )
    +
    +    // a -> 1000, 3000, 6000, 10000, 15000, 15000
    +    // b -> 0, 2000, 5000, 9000, 9000, 9000
    +    // c -> 1000, 1000, 3000, 3000, 3000, 3000
    +
    +    val outputData: Seq[Seq[(String, StreamingState)]] = Seq(
    +        Seq(
    +          ("a", 1000L),
    +          ("c", 0L)), // t = 1000
    +        Seq(
    +          ("a", 3000L),
    +          ("b", 2000L),
    +          ("c", 0L)), // t = 2000
    +        Seq(
    +          ("a", 6000L),
    +          ("b", 5000L),
    +          ("c", 3000L)), // t = 3000
    +        Seq(
    +          ("a", 10000L),
    +          ("b", 9000L),
    +          ("c", 3000L)), // t = 4000
    +        Seq(
    +          ("a", 15000L),
    +          ("b", 9000L),
    +          ("c", 3000L)), // t = 5000
    +        Seq(
    +          ("a", 15000L),
    +          ("b", 9000L),
    +          ("c", 3000L)) // t = 6000
    +      )
    +
    +    val updateStateOperation = (s: DStream[String]) => {
    +      val initialRDD = s.context.sparkContext.makeRDD(initial)
    +      val updateFunc = (time: Time,
    +                        key: String,
    +                        values: Seq[Int],
    +                        state: Option[StreamingState]) => {
    +        // Update only if we receive values for this key during the batch.
    +        if (values.nonEmpty) {
    +          Option(time.milliseconds + state.getOrElse(0L))
    +        } else {
    +          Option(state.getOrElse(0L))
    +        }
    +      }
    +      val x = s.map(x => (x, 1))
    +        x.updateStateByKey[StreamingState](updateFunc = updateFunc,
    --- End diff --
    
    nit: not need to create `x`. Just `s.map(x => (x, 1)).updateStateByKey`


---
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]

Reply via email to