title: Spark on Java - What is the right way to have a static object on all workers
body: <p>I need to use a non-serialisable 3rd party class in my functions on all executors in Spark, for example:</p>
  
  <pre><code>JavaRDD&lt;String&gt; resRdd = origRdd
      .flatMap(new FlatMapFunction&lt;String, String&gt;() {
          @Override
          public Iterable&lt;String&gt; call(String t) throws Exception {
  
          //A DynamoDB mapper I don't want to initialise every time
          DynamoDBMapper mapper = new DynamoDBMapper(new AmazonDynamoDBClient(credentials));
  
          Set&lt;String&gt; userFav = mapper.load(userDataDocument.class, userId).getFav();
  
          return userFav;
      }
  });
  </code></pre>
  
  <p>I would like to have a static <code>DynamoDBMapper mapper</code> which I initialise once for every executor and be able to use it over and over again.</p>
  
  <p>Since it's not a serialisable, I can't initialise it once in the drive and broadcast it.</p>
  
  <p>note: this is an answer here (<a href="https://stackoverflow.com/questions/26369916/what-is-the-right-way-to-have-a-static-object-on-all-workers">What is the right way to have a static object on all workers</a>) but it's only for Scala.</p>