4

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
                    }
                });
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Shankar
  • 8,529
  • 26
  • 90
  • 159

2 Answers2

7

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;
                }
            });
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
1

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

The pipe character should be escaped.

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

See: https://stackoverflow.com/a/9808719/3662739

Community
  • 1
  • 1
zeroTAG
  • 65
  • 8
  • actually i have escaped the pipe character..anyway thanks for that. – Shankar Oct 06 '15 at 11:43
  • oh, nvm then xD, I thought that would make the array break and that's why you couldn't skip. – zeroTAG Oct 06 '15 at 11:46
  • 3
    While this is a reasonable comment to the question, it's a *comment*, not an answer. Please avoid posting comments as answers. When you'll earn reputation 50, you will be able to post comments as well. – Tagir Valeev Oct 06 '15 at 11:46