Packages

trait TestGroupState[S] extends GroupState[S]

:: Experimental ::

The extended version of GroupState interface with extra getters of state machine fields to improve testability of the GroupState implementations which inherit from the extended interface.

Scala example of using TestGroupState:

// Please refer to ScalaDoc of `GroupState` for the Scala definition of `mappingFunction()`

import org.apache.spark.api.java.Optional
import org.apache.spark.sql.streaming.GroupStateTimeout
import org.apache.spark.sql.streaming.TestGroupState
// other imports

// test class setups

test("MapGroupsWithState state transition function") {
  // Creates the prevState input for the state transition function
  // with desired configs. The `create()` API would guarantee that
  // the generated instance has the same behavior as the one built by
  // engine with the same configs.
  val prevState = TestGroupState.create[Int](
    optionalState = Optional.empty[Int],
    timeoutConf = NoTimeout,
    batchProcessingTimeMs = 1L,
    eventTimeWatermarkMs = Optional.of(1L),
    hasTimedOut = false)

  val key: String = ...
  val values: Iterator[Int] = ...

  // Asserts the prevState is in init state without updates.
  assert(!prevState.isUpdated)

  // Calls the state transition function with the test previous state
  // with desired configs.
  mappingFunction(key, values, prevState)

  // Asserts the test GroupState object has been updated but not removed
  // after calling the state transition function
  assert(prevState.isUpdated)
  assert(!prevState.isRemoved)
}

Java example of using TestGroupSate:

// Please refer to ScalaDoc of `GroupState` for the Java definition of `mappingFunction()`

import org.apache.spark.api.java.Optional;
import org.apache.spark.sql.streaming.GroupStateTimeout;
import org.apache.spark.sql.streaming.TestGroupState;
// other imports

// test class setups

// test `MapGroupsWithState` state transition function `mappingFunction()`
public void testMappingFunctionWithTestGroupState() {
  // Creates the prevState input for the state transition function
  // with desired configs. The `create()` API would guarantee that
  // the generated instance has the same behavior as the one built by
  // engine with the same configs.
  TestGroupState<Int> prevState = TestGroupState.create(
    Optional.empty(),
    GroupStateTimeout.NoTimeout(),
    1L,
    Optional.of(1L),
    false);

  String key = ...;
  Integer[] values = ...;

  // Asserts the prevState is in init state without updates.
  Assert.assertFalse(prevState.isUpdated());

  // Calls the state transition function with the test previous state
  // with desired configs.
  mappingFunction.call(key, Arrays.asList(values).iterator(), prevState);

  // Asserts the test GroupState object has been updated but not removed
  // after calling the state transition function
  Assert.assertTrue(prevState.isUpdated());
  Assert.assertFalse(prevState.isRemoved());
}
S

User-defined type of the state to be stored for each group. Must be encodable into Spark SQL types (see Encoder for more details).

Annotations
@Experimental() @Evolving()
Since

3.2.0

Linear Supertypes
GroupState[S], LogicalGroupState[S], AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestGroupState
  2. GroupState
  3. LogicalGroupState
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def exists: Boolean
    Definition Classes
    GroupState
  2. abstract def get: S
    Definition Classes
    GroupState
    Annotations
    @throws("when state does not exist")
  3. abstract def getCurrentProcessingTimeMs(): Long
    Definition Classes
    GroupState
  4. abstract def getCurrentWatermarkMs(): Long
    Definition Classes
    GroupState
    Annotations
    @throws("if watermark has not been set before in [map|flatMap]GroupsWithState")
  5. abstract def getOption: Option[S]
    Definition Classes
    GroupState
  6. abstract def getTimeoutTimestampMs: Optional[Long]

    Returns the timestamp if setTimeoutTimestamp() is called.

    Returns the timestamp if setTimeoutTimestamp() is called. Or, returns batch processing time + the duration when setTimeoutDuration() is called.

    Otherwise, returns Optional.empty if not set.

  7. abstract def hasTimedOut: Boolean
    Definition Classes
    GroupState
  8. abstract def isRemoved: Boolean

    Whether the state has been marked for removing

  9. abstract def isUpdated: Boolean

    Whether the state has been updated but not removed

  10. abstract def remove(): Unit
    Definition Classes
    GroupState
  11. abstract def setTimeoutDuration(duration: String): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if \'duration\' is not a valid duration") @throws("if processing time timeout has not been enabled in [map|flatMap]GroupsWithState")
  12. abstract def setTimeoutDuration(durationMs: Long): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if \'durationMs\' is not positive") @throws("if processing time timeout has not been enabled in [map|flatMap]GroupsWithState")
  13. abstract def setTimeoutTimestamp(timestamp: Date, additionalDuration: String): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if \'additionalDuration\' is invalid") @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
  14. abstract def setTimeoutTimestamp(timestamp: Date): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
  15. abstract def setTimeoutTimestamp(timestampMs: Long, additionalDuration: String): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if \'additionalDuration\' is invalid or the final timeout timestamp is less than the current watermark in a streaming query") @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
  16. abstract def setTimeoutTimestamp(timestampMs: Long): Unit
    Definition Classes
    GroupState
    Annotations
    @throws("if \'timestampMs\' is not positive or less than the current watermark in a streaming query") @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
  17. abstract def update(newState: S): Unit
    Definition Classes
    GroupState

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  9. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from GroupState[S]

Inherited from LogicalGroupState[S]

Inherited from AnyRef

Inherited from Any

Ungrouped