title: org.apache.spark.SparkException: Task not serializable - JavaSparkContext
body: <p>I'm trying to run the following simple Spark code:</p>
  
  <pre><code>Gson gson = new Gson();
  JavaRDD&lt;String&gt; stringRdd = jsc.textFile("src/main/resources/META-INF/data/supplier.json");
  
  JavaRDD&lt;SupplierDTO&gt; rdd = stringRdd.map(new Function&lt;String, SupplierDTO&gt;()
  {
      private static final long serialVersionUID = -78238876849074973L;
  
      @Override
      public SupplierDTO call(String str) throws Exception
      {
          return gson.fromJson(str, SupplierDTO.class);
      }
  });
  </code></pre>
  
  <p>But it's throwing the following error while executing the <code>stringRdd.map</code> statement:</p>
  
  <pre><code>org.apache.spark.SparkException: Task not serializable
  at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:166)
  at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:158)
  at org.apache.spark.SparkContext.clean(SparkContext.scala:1478)
  at org.apache.spark.rdd.RDD.map(RDD.scala:288)
  at org.apache.spark.api.java.JavaRDDLike$class.map(JavaRDDLike.scala:78)
  at org.apache.spark.api.java.JavaRDD.map(JavaRDD.scala:32)
  at com.demo.spark.processor.cassandra.CassandraDataUploader.uploadData(CassandraDataUploader.java:71)
  at com.demo.spark.processor.cassandra.CassandraDataUploader.main(CassandraDataUploader.java:47)
  Caused by: java.io.NotSerializableException: org.apache.spark.api.java.JavaSparkContext
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
  at org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:42)
  at org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:73)
  at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:164)
  ... 7 more
  </code></pre>
  
  <p>Here 'jsc' is the <code>JavaSparkContext</code> object I'm using.
  As far as I know, <code>JavaSparkContext</code> is not a <code>Serializable</code> object and one should not use it inside any functions which will be sent to the Spark workers.</p>
  
  <p>Now, what I'm not able to understand is, how the instance of <code>JavaSparkContext</code> is being sent to the workers? What should I change in my code to avoid such scenario?</p>