如何以最快的方式检查 DataFrame(Scala) 是否为空?我使用 DF.limit(1).rdd.isEmpty,比 DF.rdd 更快。是空的,但不理想。有没有更好的方法来做到这一点?
最佳答案
我通常将对 first
的调用包裹在 Try
中:
import scala.util.Try
val t = Try(df.first)
如果它是成功
或失败
,您可以从那里匹配它以控制逻辑:
import scala.util.{Success,Failure}
t match {
case Success(df) => //do stuff with the dataframe
case Failure(e) =>
// dataframe is empty; do other stuff
//e.getMessage will return the exception message
}
关于scala - 检查 DataFrame(Scala) 是否为空的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36998951/