You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@flink.apache.org by Flavio Pompermaier <po...@okkam.it> on 2016/06/23 13:12:36 UTC

Initialization of static variables

Hi all,
I've a Flink job that initialize a static Map in the main program, before
starting any Flink transformation. If I run the job locally that variable
is not empty, running the job on the cluster reset that variable..is it a
bug or am I doing something wrong?
It only works if I initialize that variable in a static statement before
the main, that is:

///////////////// KO EXAMPLE
class ErrorMain {

    private static final Map<String,String> ht = new HashMap<>();

    publis static final main(String[]args){
     ht.put("test","test");
     env.readFile().map(
        ...
       //here ht.get("test") returns null
  }
}

///////////////// OK EXAMPLE
class OkMain {

    private static final Map<String,String> ht = new HashMap<>();
    static{
        ht.put("test","test");
    }
    publis static final main(String[] args){

     env.readFile().map(
        ...
       //here ht.get("test") works
  }
}


Best,
Flavio

Re: Initialization of static variables

Posted by Flavio Pompermaier <po...@okkam.it>.
Ok, thanks for the explanation Till!

On Thu, Jun 23, 2016 at 3:19 PM, Till Rohrmann <tr...@apache.org> wrote:

> Yes this is normal Flink behaviour. The reason is that static variables
> are not transferred to the cluster. What happens instead when you first
> load the class on the cluster is that the static variables are created and
> possible class initializer are executed. That is also the reason why your
> second example works whereas the first fails.
>
> Cheers,
> Till
>
> On Thu, Jun 23, 2016 at 3:12 PM, Flavio Pompermaier <po...@okkam.it>
> wrote:
>
>> Hi all,
>> I've a Flink job that initialize a static Map in the main program, before
>> starting any Flink transformation. If I run the job locally that variable
>> is not empty, running the job on the cluster reset that variable..is it a
>> bug or am I doing something wrong?
>> It only works if I initialize that variable in a static statement before
>> the main, that is:
>>
>> ///////////////// KO EXAMPLE
>> class ErrorMain {
>>
>>     private static final Map<String,String> ht = new HashMap<>();
>>
>>     publis static final main(String[]args){
>>      ht.put("test","test");
>>      env.readFile().map(
>>         ...
>>        //here ht.get("test") returns null
>>   }
>> }
>>
>> ///////////////// OK EXAMPLE
>> class OkMain {
>>
>>     private static final Map<String,String> ht = new HashMap<>();
>>     static{
>>         ht.put("test","test");
>>     }
>>     publis static final main(String[] args){
>>
>>      env.readFile().map(
>>         ...
>>        //here ht.get("test") works
>>   }
>> }
>>
>>
>> Best,
>> Flavio
>>
>>
>

Re: Initialization of static variables

Posted by Till Rohrmann <tr...@apache.org>.
Yes this is normal Flink behaviour. The reason is that static variables are
not transferred to the cluster. What happens instead when you first load
the class on the cluster is that the static variables are created and
possible class initializer are executed. That is also the reason why your
second example works whereas the first fails.

Cheers,
Till

On Thu, Jun 23, 2016 at 3:12 PM, Flavio Pompermaier <po...@okkam.it>
wrote:

> Hi all,
> I've a Flink job that initialize a static Map in the main program, before
> starting any Flink transformation. If I run the job locally that variable
> is not empty, running the job on the cluster reset that variable..is it a
> bug or am I doing something wrong?
> It only works if I initialize that variable in a static statement before
> the main, that is:
>
> ///////////////// KO EXAMPLE
> class ErrorMain {
>
>     private static final Map<String,String> ht = new HashMap<>();
>
>     publis static final main(String[]args){
>      ht.put("test","test");
>      env.readFile().map(
>         ...
>        //here ht.get("test") returns null
>   }
> }
>
> ///////////////// OK EXAMPLE
> class OkMain {
>
>     private static final Map<String,String> ht = new HashMap<>();
>     static{
>         ht.put("test","test");
>     }
>     publis static final main(String[] args){
>
>      env.readFile().map(
>         ...
>        //here ht.get("test") works
>   }
> }
>
>
> Best,
> Flavio
>
>