Fastest way to check if DataFrame(Scala) is empty?

不羁岁月 提交于 2019-12-11 02:47:50

问题


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?


回答1:


I usually wrap a call to first around a Try:

import scala.util.Try

val t = Try(df.first)

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
}


来源:https://stackoverflow.com/questions/36998951/fastest-way-to-check-if-dataframescala-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!