Apache Spark RDD and Java 8: Exception Handling

I need to skip recording, if I get any exception, iterating the contents of the file using Java 8 and Spark.

I don’t want to throw an exception, I just need to skip this entry and continue with other entries.

Code example:

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
                    }
                });
+4
source share
2 answers

You can use filterinstead 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;
                }
            });
+5
source

String[] parts = line.split("|");

The pipe symbol must be escaped.

String[] parts = line.split("\\|");

See: fooobar.com/questions/43170 / ...

+1
source

Source: https://habr.com/ru/post/1610414/


All Articles