Spark如何检查DataFrame/RDD是否已缓存

【方法1:在spark UI中查看】

在spark脚本运行后,打开spark UI的Storage界面,便能看到当前已缓存的所有rdd

【方法2:利用tempView和catalog】

先把数据注册为临时表,然后可以通过catalog来检查临时表是否已缓存

package high_quality._history

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.SparkSession

object test {

  def main(args: Array[String]) {

    Logger.getRootLogger.setLevel(Level.ERROR)
    val spark = SparkSession.builder().master("local[*]").getOrCreate()
    import spark.implicits._

    // 构造一个DataFrame
    val df = Seq("1").toDF("value")
    df.createTempView("tmp_df")

    // 创建一个catalog
    val catalog = spark.catalog

    // 打印所有数据库的名称
    catalog.listDatabases().select("name").show()

    // 打印所有临时表的名称
    catalog.listTables().select("name").show()

    // 检查某个临时表是否已经缓存
    println(catalog.isCached("tmp_df"))
    df.cache()
    println(catalog.isCached("tmp_df"))
  }
}

该脚本的stdout:

+-------+
|   name|
+-------+
|default|
+-------+

+------+
|  name|
+------+
|tmp_df|
+------+

false
true
发布了70 篇原创文章 · 获赞 17 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/hwj_wayne/article/details/104069977
今日推荐