本文介绍了检查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)

如果是控制逻辑的成功 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)是否为空的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 11:48
查看更多