Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Spark RDD and Java 8: Exception handling

I need to skip the record, if i get any exception while iterating the file content using Java 8 and Spark.

I do not want to throw exception, i just need to skip that record and continue with other records.

Code Example is :

JavaRDD<Model> fileRDD = sc.textFile("filePath")
                .map(line -> {
                    try {
                    String[] parts = line.split("\\|");
                    Long key = Long.parseLong(parts[0];
                    return line;
                    } catch (NumberFormatException nfe) {
                        //if i throw RuntimeException, its working file
                        //but i dont want to throw exception, i want to just skip the line,
                        // how do i do it using java 8 stream methods
                    }
                });
like image 462
Shankar Avatar asked Feb 08 '23 21:02

Shankar


1 Answers

You can use filter instead of map:

JavaRDD<Model> fileRDD = sc.textFile("filePath")
            .filter(line -> {
                try {
                    String[] parts = line.split("\\|");
                    Long key = Long.parseLong(parts[0];
                    return true;
                } catch (NumberFormatException nfe) {
                    return false;
                }
            });
like image 59
Tagir Valeev Avatar answered Feb 11 '23 14:02

Tagir Valeev