Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DBC59200B32 for ; Thu, 23 Jun 2016 15:19:07 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id DA3C9160A59; Thu, 23 Jun 2016 13:19:07 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2F797160A58 for ; Thu, 23 Jun 2016 15:19:07 +0200 (CEST) Received: (qmail 37560 invoked by uid 500); 23 Jun 2016 13:19:06 -0000 Mailing-List: contact user-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@flink.apache.org Delivered-To: mailing list user@flink.apache.org Received: (qmail 37547 invoked by uid 99); 23 Jun 2016 13:19:06 -0000 Received: from mail-relay.apache.org (HELO mail-relay.apache.org) (140.211.11.15) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jun 2016 13:19:06 +0000 Received: from mail-wm0-f49.google.com (mail-wm0-f49.google.com [74.125.82.49]) by mail-relay.apache.org (ASF Mail Server at mail-relay.apache.org) with ESMTPSA id E20951A022A for ; Thu, 23 Jun 2016 13:19:05 +0000 (UTC) Received: by mail-wm0-f49.google.com with SMTP id a66so49818041wme.0 for ; Thu, 23 Jun 2016 06:19:05 -0700 (PDT) X-Gm-Message-State: ALyK8tJJwNJxk1eupyM4i+Sj0fsgznb65GugfVHTp4HXBlhBmT/22v18sJfu41mXMeseGHLfuzlGkjfeqkq1+Q== X-Received: by 10.28.221.4 with SMTP id u4mr829265wmg.32.1466687944454; Thu, 23 Jun 2016 06:19:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.242.99 with HTTP; Thu, 23 Jun 2016 06:19:03 -0700 (PDT) In-Reply-To: References: From: Till Rohrmann Date: Thu, 23 Jun 2016 15:19:03 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Initialization of static variables To: user@flink.apache.org Content-Type: multipart/alternative; boundary=001a1148e4d6ecfa5f0535f1e507 archived-at: Thu, 23 Jun 2016 13:19:08 -0000 --001a1148e4d6ecfa5f0535f1e507 Content-Type: text/plain; charset=UTF-8 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 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 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 ht = new HashMap<>(); > static{ > ht.put("test","test"); > } > publis static final main(String[] args){ > > env.readFile().map( > ... > //here ht.get("test") works > } > } > > > Best, > Flavio > > --001a1148e4d6ecfa5f0535f1e507 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Yes this is normal Flink behaviour. The reason is that sta= tic 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 c= reated 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 <p= ompermaier@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 ru= n the job locally that variable is not empty, running the job on the cluste= r 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 t= he main, that is:

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

=C2=A0 =C2=A0 private sta= tic final Map<String,String> ht =3D new HashMap<>();
=
=C2=A0 =C2=A0 publis static final main(String[]args){
<= div>=C2=A0 =C2=A0 =C2=A0ht.put("test","test");
=C2=A0 =C2=A0 =C2=A0env.readFile().map(
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 ...
=C2=A0 =C2=A0 =C2=A0 =C2=A0//here ht.get("test&qu= ot;) returns null
=C2=A0 }
}

/= //////////////// OK EXAMPLE
class OkMain {
=C2=A0 =C2=A0 private static final Map<String,String> ht = =3D new HashMap<>();
=C2=A0 =C2=A0 static{
=C2=A0= =C2=A0 =C2=A0 =C2=A0 ht.put("test","test");
<= div>=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 publis static final main(String= [] args){
=C2=A0 =C2=A0=C2=A0
=C2=A0 =C2=A0 =C2=A0env.r= eadFile().map(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ...
=C2=A0 = =C2=A0 =C2=A0 =C2=A0//here ht.get("test") works
=C2=A0 = }
}


Best,
Flavio
=


--001a1148e4d6ecfa5f0535f1e507--