Initial value of ValueStates of primitive types

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Initial value of ValueStates of primitive types

Averell
Hi,

In Flink's documents, I couldn't find any example that uses primitive type
when working with States. What would be the initial value of a ValueState of
type Int/Boolean/...? The same question apply for MapValueState like
[String, Int]

Thanks and regards,
Averell



--
Sent from: http://apache-flink-user-mailing-list-archive.2336050.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Initial value of ValueStates of primitive types

Dominik Wosiński
Hey,

After you call, by default values you mean after you call : 
getRuntimeContext.getState()
If so, the default value will be state with value() of null, as described in :
/**
* Returns the current value for the state. When the state is not
* partitioned the returned value is the same for all inputs in a given
* operator instance. If state partitioning is applied, the value returned
* depends on the current operator input, as the operator maintains an
* independent state for each partition.
*
* <p>If you didn't specify a default value when creating the {@link ValueStateDescriptor}
* this will return {@code null} when to value was previously set using {@link #update(Object)}.
*
* @return The state value corresponding to the current input.
*
* @throws IOException Thrown if the system cannot access the state.
*/
T value() throws IOException;
For the MapState it should be an empty map with no keys present. 

Funny thing is that there is an implicit conversion between null values returned by state, so assume you have defined :

private lazy val test: ValueState[Boolean] = getRuntimeContext.getState(new ValueStateDescriptor[Boolean]("test", classOf[Boolean]))
If you will now do : 
print(test.value())
It will indeed print the null.
But if You will do  :
val myTest = test.value()
print(test.value())
It will now print false instead;

Best Regards,
Dominik.

2018-08-17 11:13 GMT+02:00 Averell <[hidden email]>:
Hi,

In Flink's documents, I couldn't find any example that uses primitive type
when working with States. What would be the initial value of a ValueState of
type Int/Boolean/...? The same question apply for MapValueState like
[String, Int]

Thanks and regards,
Averell



--
Sent from: http://apache-flink-user-mailing-list-archive.2336050.n4.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Initial value of ValueStates of primitive types

Averell
Thank you Dominik.

So there's an implicit conversion, which means that getState().value() would
always give a deteministic result (i.e: Boolean value would always be false,
Int value would always be 0)

I found another funny thing is even with ref type like Integer, there is
also that implicit conversion:

val y:Integer = getRuntimeContext.getState(new
ValueStateDescriptor[Int]("Triggered", classOf[Int])).value()
>> y = {Integer@5795} "0"

Thanks for your time.
Regards,
Averell



--
Sent from: http://apache-flink-user-mailing-list-archive.2336050.n4.nabble.com/