I. Introduction
When Spark uses RDD to call the Filter function, the driver side gets stuck and reports the error ReturnStatementInClosureException: Return statements aren't allowed in Spark closures, that is, the return function cannot be used in the closure:
2. Usage scenarios
The return method is used when using the rdd.filter method to filter the id, resulting in the above error:
rdd.filter(arr => {
val id = arr(0)
val l = id.length()
if (l <= 8) return false
if (id.startsWith("1")) {
true
} else {
false
}
})
amendment:
The return keyword is not used in the closure function.
rdd.filter(arr => {
val id = arr(0)
val l = id.length()
if (l <= 8) {
false
} else if (id.startsWith("1")) {
true
} else {
false
}
})