trait GroupState[S] extends LogicalGroupState[S]
:: Experimental ::
Wrapper class for interacting with per-group state data in mapGroupsWithState
and
flatMapGroupsWithState
operations on KeyValueGroupedDataset
.
Detail description on [map/flatMap]GroupsWithState
operation
-------------------------------------------------------------- Both, mapGroupsWithState
and
flatMapGroupsWithState
in KeyValueGroupedDataset
will invoke the user-given function on
each group (defined by the grouping function in Dataset.groupByKey()
) while maintaining a
user-defined per-group state between invocations. For a static batch Dataset, the function will
be invoked once per group. For a streaming Dataset, the function will be invoked for each group
repeatedly in every trigger. That is, in every batch of the StreamingQuery
, the function will
be invoked once for each group that has data in the trigger. Furthermore, if timeout is set,
then the function will be invoked on timed-out groups (more detail below).
The function is invoked with the following parameters.
- The key of the group.
- An iterator containing all the values for this group.
- A user-defined state object set by previous invocations of the given function.
In case of a batch Dataset, there is only one invocation and the state object will be empty as
there is no prior state. Essentially, for batch Datasets, [map/flatMap]GroupsWithState
is
equivalent to [map/flatMap]Groups
and any updates to the state and/or timeouts have no
effect.
The major difference between mapGroupsWithState
and flatMapGroupsWithState
is that the
former allows the function to return one and only one record, whereas the latter allows the
function to return any number of records (including no records). Furthermore, the
flatMapGroupsWithState
is associated with an operation output mode, which can be either
Append
or Update
. Semantically, this defines whether the output records of one trigger is
effectively replacing the previously output records (from previous triggers) or is appending to
the list of previously output records. Essentially, this defines how the Result Table (refer to
the semantics in the programming guide) is updated, and allows us to reason about the semantics
of later operations.
Important points to note about the function (both mapGroupsWithState and flatMapGroupsWithState).
- In a trigger, the function will be called only the groups present in the batch. So do not assume that the function will be called in every trigger for every group that has state.
- There is no guaranteed ordering of values in the iterator in the function, neither with batch, nor with streaming Datasets.
- All the data will be shuffled before applying the function.
- If timeout is set, then the function will also be called with no values. See more details
on
GroupStateTimeout
below.
Important points to note about using GroupState
.
- The value of the state cannot be null. So updating state with null will throw
IllegalArgumentException
. - Operations on
GroupState
are not thread-safe. This is to avoid memory barriers. - If
remove()
is called, thenexists()
will returnfalse
,get()
will throwNoSuchElementException
andgetOption()
will returnNone
- After that, if
update(newState)
is called, thenexists()
will again returntrue
,get()
andgetOption()
will return the updated value.
Important points to note about using GroupStateTimeout
.
- The timeout type is a global param across all the groups (set as
timeout
param in[map|flatMap]GroupsWithState
, but the exact timeout duration/timestamp is configurable per group by callingsetTimeout...()
inGroupState
. - Timeouts can be either based on processing time (i.e.
GroupStateTimeout.ProcessingTimeTimeout
) or event time (i.e.GroupStateTimeout.EventTimeTimeout
). - With
ProcessingTimeTimeout
, the timeout duration can be set by callingGroupState.setTimeoutDuration
. The timeout will occur when the clock has advanced by the set duration. Guarantees provided by this timeout with a duration of D ms are as follows:- Timeout will never occur before the clock time has advanced by D ms
- Timeout will occur eventually when there is a trigger in the query (i.e. after D ms). So there is no strict upper bound on when the timeout would occur. For example, the trigger interval of the query will affect when the timeout actually occurs. If there is no data in the stream (for any group) for a while, then there will not be any trigger and timeout function call will not occur until there is data.
- Since the processing time timeout is based on the clock time, it is affected by the variations in the system clock (i.e. time zone changes, clock skew, etc.).
- With
EventTimeTimeout
, the user also has to specify the event time watermark in the query usingDataset.withWatermark()
. With this setting, data that is older than the watermark is filtered out. The timeout can be set for a group by setting a timeout timestamp usingGroupState.setTimeoutTimestamp()
, and the timeout would occur when the watermark advances beyond the set timestamp. You can control the timeout delay by two parameters - (i) watermark delay and an additional duration beyond the timestamp in the event (which is guaranteed to be newer than watermark due to the filtering). Guarantees provided by this timeout are as follows:- Timeout will never occur before the watermark has exceeded the set timeout.
- Similar to processing time timeouts, there is no strict upper bound on the delay when the timeout actually occurs. The watermark can advance only when there is data in the stream and the event time of the data has actually advanced.
- When the timeout occurs for a group, the function is called for that group with no values,
and
GroupState.hasTimedOut()
set to true. - The timeout is reset every time the function is called on a group, that is, when the group has new data, or the group has timed out. So the user has to set the timeout duration every time the function is called, otherwise, there will not be any timeout set.
[map/flatMap]GroupsWithState
can take a user defined initial state as an additional argument.
This state will be applied when the first batch of the streaming query is processed. If there
are no matching rows in the data for the keys present in the initial state, the state is still
applied and the function will be invoked with the values being an empty iterator.
Scala example of using GroupState in mapGroupsWithState
:
// A mapping function that maintains an integer state for string keys and returns a string. // Additionally, it sets a timeout to remove the state if it has not received data for an hour. def mappingFunction(key: String, value: Iterator[Int], state: GroupState[Int]): String = { if (state.hasTimedOut) { // If called when timing out, remove the state state.remove() } else if (state.exists) { // If state exists, use it for processing val existingState = state.get // Get the existing state val shouldRemove = ... // Decide whether to remove the state if (shouldRemove) { state.remove() // Remove the state } else { val newState = ... state.update(newState) // Set the new state state.setTimeoutDuration("1 hour") // Set the timeout } } else { val initialState = ... state.update(initialState) // Set the initial state state.setTimeoutDuration("1 hour") // Set the timeout } ... // return something } dataset .groupByKey(...) .mapGroupsWithState(GroupStateTimeout.ProcessingTimeTimeout)(mappingFunction)
Java example of using GroupState
:
// A mapping function that maintains an integer state for string keys and returns a string. // Additionally, it sets a timeout to remove the state if it has not received data for an hour. MapGroupsWithStateFunction<String, Integer, Integer, String> mappingFunction = new MapGroupsWithStateFunction<String, Integer, Integer, String>() { @Override public String call(String key, Iterator<Integer> value, GroupState<Integer> state) { if (state.hasTimedOut()) { // If called when timing out, remove the state state.remove(); } else if (state.exists()) { // If state exists, use it for processing int existingState = state.get(); // Get the existing state boolean shouldRemove = ...; // Decide whether to remove the state if (shouldRemove) { state.remove(); // Remove the state } else { int newState = ...; state.update(newState); // Set the new state state.setTimeoutDuration("1 hour"); // Set the timeout } } else { int initialState = ...; // Set the initial state state.update(initialState); state.setTimeoutDuration("1 hour"); // Set the timeout } ... // return something } }; dataset .groupByKey(...) .mapGroupsWithState( mappingFunction, Encoders.INT, Encoders.STRING, GroupStateTimeout.ProcessingTimeTimeout);
- 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()
- Source
- GroupState.scala
- Since
2.2.0
- Alphabetic
- By Inheritance
- GroupState
- LogicalGroupState
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def exists: Boolean
Whether state exists or not.
- abstract def get: S
Get the state value if it exists, or throw NoSuchElementException.
Get the state value if it exists, or throw NoSuchElementException.
- Annotations
- @throws("when state does not exist")
- abstract def getCurrentProcessingTimeMs(): Long
Get the current processing time as milliseconds in epoch time.
Get the current processing time as milliseconds in epoch time.
- Note
In a streaming query, this will return a constant value throughout the duration of a trigger, even if the trigger is re-executed.
- abstract def getCurrentWatermarkMs(): Long
Get the current event time watermark as milliseconds in epoch time.
Get the current event time watermark as milliseconds in epoch time.
- Annotations
- @throws("if watermark has not been set before in [map|flatMap]GroupsWithState")
- Note
In a streaming query, this can be called only when watermark is set before calling
,[map/flatMap]GroupsWithState
. In a batch query, this method always returns -1.The watermark gets propagated in the end of each query. As a result, this method will return 0 (1970-01-01T00:00:00) for the first micro-batch. If you use this value as a part of the timestamp set in the
setTimeoutTimestamp
, it may lead to the state expiring immediately in the next micro-batch, once the watermark gets the real value from your data.
- abstract def getOption: Option[S]
Get the state value as a scala Option.
- abstract def hasTimedOut: Boolean
Whether the function has been called because the key has timed out.
Whether the function has been called because the key has timed out.
- Note
This can return true only when timeouts are enabled in
[map/flatMap]GroupsWithState
.
- abstract def remove(): Unit
Remove this state.
- abstract def setTimeoutDuration(duration: String): Unit
Set the timeout duration for this key as a string.
Set the timeout duration for this key as a string. For example, "1 hour", "2 days", etc.
- Annotations
- @throws("if 'duration' is not a valid duration") @throws("if processing time timeout has not been enabled in [map|flatMap]GroupsWithState")
- Note
Processing time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no effect when used in a batch query.
- abstract def setTimeoutDuration(durationMs: Long): Unit
Set the timeout duration in ms for this key.
Set the timeout duration in ms for this key.
- Annotations
- @throws("if 'durationMs' is not positive") @throws("if processing time timeout has not been enabled in [map|flatMap]GroupsWithState")
- Note
Processing time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no effect when used in a batch query.
- abstract def setTimeoutTimestamp(timestamp: Date, additionalDuration: String): Unit
Set the timeout timestamp for this key as a java.sql.Date and an additional duration as a string (e.g.
Set the timeout timestamp for this key as a java.sql.Date and an additional duration as a string (e.g. "1 hour", "2 days", etc.). The final timestamp (including the additional duration) cannot be older than the current watermark.
- Annotations
- @throws("if 'additionalDuration' is invalid") @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
- Note
Event time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no side effect when used in a batch query.
- abstract def setTimeoutTimestamp(timestamp: Date): Unit
Set the timeout timestamp for this key as a java.sql.Date.
Set the timeout timestamp for this key as a java.sql.Date. This timestamp cannot be older than the current watermark.
- Annotations
- @throws("if event time timeout has not been enabled in [map|flatMap]GroupsWithState")
- Note
Event time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no side effect when used in a batch query.
- abstract def setTimeoutTimestamp(timestampMs: Long, additionalDuration: String): Unit
Set the timeout timestamp for this key as milliseconds in epoch time and an additional duration as a string (e.g.
Set the timeout timestamp for this key as milliseconds in epoch time and an additional duration as a string (e.g. "1 hour", "2 days", etc.). The final timestamp (including the additional duration) cannot be older than the current watermark.
- 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") - Note
Event time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no side effect when used in a batch query.
- abstract def setTimeoutTimestamp(timestampMs: Long): Unit
Set the timeout timestamp for this key as milliseconds in epoch time.
Set the timeout timestamp for this key as milliseconds in epoch time. This timestamp cannot be older than the current watermark.
- 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")
- Note
Event time timeout must be enabled in
,[map/flatMap]GroupsWithState
for calling this method.This method has no effect when used in a batch query.
- abstract def update(newState: S): Unit
Update the value of the state.
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @IntrinsicCandidate() @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @IntrinsicCandidate() @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @IntrinsicCandidate() @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @IntrinsicCandidate() @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @IntrinsicCandidate() @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated
(Since version 9)