如果我在使用 Java 8 和 Spark 迭代文件内容时遇到任何异常,我需要跳过记录。
我不想抛出异常,我只需要跳过那条记录并继续其他记录。
代码示例是:
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
}
});
最佳答案
您可以使用 filter
而不是 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;
}
});
关于java - Apache Spark RDD 和 Java 8 : Exception handling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968881/