RDD is not Cassandra / Spark java API serializable API

so I had some questions on how to query cassandra using spark in java maven project here: Query data in Cassandra via Spark in Java Maven project

Well my question was answered and worked, however I got a problem (possibly a problem). I am trying to use the datastax java API. Here is my code:

package com.angel.testspark.test2;

import org.apache.commons.lang3.StringUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

import java.io.Serializable;

import static com.datastax.spark.connector.CassandraJavaUtil.*;

public class App 

    // firstly, we define a bean class
    public static class Person implements Serializable {
        private Integer id;
        private String fname;
        private String lname;
        private String role;

        // Remember to declare no-args constructor
        public Person() { }

        public Integer getId() { return id; }
        public void setId(Integer id) { this.id = id; }

        public String getfname() { return fname; }
        public void setfname(String fname) { this.fname = fname; }

        public String getlname() { return lname; }
        public void setlname(String lname) { this.lname = lname; }

        public String getrole() { return role; }
        public void setrole(String role) { this.role = role; }

        // other methods, constructors, etc.

    private transient SparkConf conf;
    private App(SparkConf conf) {
        this.conf = conf;

    private void run() {
        JavaSparkContext sc = new JavaSparkContext(conf);


    private void createSchema(JavaSparkContext sc) {

        JavaRDD<String> rdd = javaFunctions(sc).cassandraTable("tester", "empbyrole", Person.class)
                .where("role=?", "IT Engineer").map(new Function<Person, String>() {
                    public String call(Person person) throws Exception {
                        return person.toString();
        System.out.println("Data as Person beans: \n" + StringUtils.join("\n", rdd.toArray()));

    public static void main( String[] args )
        if (args.length != 2) {
            System.err.println("Syntax: com.datastax.spark.demo.JavaDemo <Spark Master URL> <Cassandra contact point>");

        SparkConf conf = new SparkConf();
        conf.setAppName("Java API demo");
        conf.set("spark.cassandra.connection.host", args[1]);

        App app = new App(conf);


here is my error:

Exception in thread "main" org.apache.spark.SparkException: Job aborted: Task not serializable: java.io.NotSerializableException: com.angel.testspark.test2.App
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$org$apache$spark$scheduler$DAGScheduler$$abortStage$1.apply(DAGScheduler.scala:1020)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$org$apache$spark$scheduler$DAGScheduler$$abortStage$1.apply(DAGScheduler.scala:1018)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$abortStage(DAGScheduler.scala:1018)
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$submitMissingTasks(DAGScheduler.scala:781)
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$submitStage(DAGScheduler.scala:724)
    at org.apache.spark.scheduler.DAGScheduler.processEvent(DAGScheduler.scala:554)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$start$1$$anon$2$$anonfun$receive$1.applyOrElse(DAGScheduler.scala:190)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:498)
    at akka.actor.ActorCell.invoke(ActorCell.scala:456)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:237)
    at akka.dispatch.Mailbox.run(Mailbox.scala:219)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)


Now I KNOW where my mistake is. This System.out.println("Data as Person beans: \n" + StringUtils.join("\n", rdd.toArray()));

is because I need to convert rdd to an array. However, the SAID API documentation should be able to do this ... this is the code copied and pasted from the documentation. Why can't I serialize RDD to array?

I have already inserted dummy data into my cassandra using the inserts in my post which I have included in the link above.

Also, the previous error that I resolved was that I changed all my getters and setters to lower case. When I used capitals in them, it caused an error. Why can't I use capitals in my getters and setters here?

Thanks, Angel


source to share

1 answer

Changing public class App

to public class App implements Serializable

should correct the error. Since the inner java class will keep a reference to the outer class, your object Function

will have a reference to App

. Since Spark needs to serialize your object Function

, it requires it to App

be serializable as well.



All Articles