# Exception while inserting data in to elastic search using apache spark (org.apache.spark.SparkException: Task not serializable)

**URL:** <https://discuss.elastic.co/t/exception-while-inserting-data-in-to-elastic-search-using-apache-spark-org-apache-spark-sparkexception-task-not-serializable/122352>\
**Category:** Elasticsearch\
**Created:** [March 3, 2018, 5:23am UTC](https://discuss.elastic.co/t/exception-while-inserting-data-in-to-elastic-search-using-apache-spark-org-apache-spark-sparkexception-task-not-serializable/122352 "2018-03-03T05:23:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Basim](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Basim](https://discuss.elastic.co/u/Basim)\
**Post date:** [March 3, 2018, 5:23am UTC](https://discuss.elastic.co/t/exception-while-inserting-data-in-to-elastic-search-using-apache-spark-org-apache-spark-sparkexception-task-not-serializable/122352/1 "2018-03-03T05:23:14Z")

</div>

I am using custom spark receiver for spark streaming. In javaCustomStream i am getting value in JavaDStream But when i try to save data into elasticsearch by using saveJsonToEs function it gives exception Apache spark exception org.apache.spark.SparkException: Task not serializable  
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)

```
    spark.getCustomReceiverStream().foreachRDD(rdd -> {
            rdd.foreachPartition (partition -> {
                partition.forEachRemaining(row -> {
                    LOG.debug("printing here : " + row.toString());  
                
				// Now when i try to perform some action on row it gives exception
				
                });
            });
        });
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 31, 2018, 5:23am UTC](https://discuss.elastic.co/t/exception-while-inserting-data-in-to-elastic-search-using-apache-spark-org-apache-spark-sparkexception-task-not-serializable/122352/2 "2018-03-31T05:23:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
