[英]Apache Spark RDD and Java 8: Exception handling
如果在使用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;
}
});
String[] parts = line.split("|");
管道角色應該被轉義。
String[] parts = line.split("\\\\|");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.