Task not Serializable - Spark Java
<p>I'm getting the Task not serializable error in Spark. I've searched and tried to use a static function as suggested in some posts, but it still gives the same error. </p>

<p>Code is as below:</p>

<pre><code>public class Rating implements Serializable {
    private SparkSession spark;
    private SparkConf sparkConf;
    private JavaSparkContext jsc;
    private static Function&lt;String, Rating&gt; mapFunc;

    public Rating() {
        mapFunc = new Function&lt;String, Rating&gt;() {
            public Rating call(String str) {
                return Rating.parseRating(str);
            }
        };
    }

    public void runProcedure() { 
        sparkConf = new SparkConf().setAppName("Filter Example").setMaster("local");
        jsc = new JavaSparkContext(sparkConf);
        SparkSession spark = SparkSession.builder().master("local").appName("Word Count")
            .config("spark.some.config.option", "some-value").getOrCreate();        

        JavaRDD&lt;Rating&gt; ratingsRDD = spark.read().textFile("sample_movielens_ratings.txt")
                .javaRDD()
                .map(mapFunc);
    }

    public static void main(String[] args) {
        Rating newRating = new Rating();
        newRating.runProcedure();
    }
}
</code></pre>

<p>The error gives:
<a href="https://i.stack.imgur.com/RxwuW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RxwuW.png" alt="enter image description here"></a></p>

<p>How do I solve this error?
Thanks in advance.</p>