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
}
});
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With