You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@flink.apache.org by Averell <lv...@gmail.com> on 2018/08/17 09:13:51 UTC
Initial value of ValueStates of primitive types
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/
Re: Initial value of ValueStates of primitive types
Posted by Averell <lv...@gmail.com>.
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/
Re: Initial value of ValueStates of primitive types
Posted by Dominik WosiĆski <wo...@gmail.com>.
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 <lv...@gmail.com>:
> 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/
>