检查 DataFrame(Scala) 是否为空的最快方法? [英] Fastest way to check if DataFrame(Scala) is empty?
本文介绍了检查 DataFrame(Scala) 是否为空的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以最快的方式检查 DataFrame(Scala) 是否为空?我使用 DF.limit(1).rdd.isEmpty,比 DF.rdd 快.isEmpty,但不理想.有没有更好的方法来做到这一点?
How to check if DataFrame(Scala) is empty in fastest way?I use DF.limit(1).rdd.isEmpty, faster than DF.rdd.isEmpty,but not ideal.Is there any better way to do that?
推荐答案
我通常将 first
的调用包装在 Try
周围:
I usually wrap a call to first
around a Try
:
import scala.util.Try
val t = Try(df.first)
如果控制逻辑是 Success
或 Failure
,你可以从那里匹配它:
From there you can match on it if it's a Success
or Failure
to control logic:
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
}
这篇关于检查 DataFrame(Scala) 是否为空的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文