对于简短的回答,我们可以看看the documentation 关于spark.local.dir:
用于 Spark 中“临时”空间的目录,包括地图输出文件和存储在磁盘上的 RDD。这应该在系统中一个快速的本地磁盘上。它也可以是不同磁盘上多个目录的逗号分隔列表。注意:在 Spark 1.0 和更高版本中,这将被集群管理器设置的 SPARK_LOCAL_DIRS(独立、Mesos)或 LOCAL_DIRS(YARN)环境变量覆盖。
为了更深入地了解我们可以查看代码:DataFrame(只是Dataset[Row])基于RDDs,它利用相同的持久性机制。 RDDs 将此委托给 SparkContext,这将其标记为持久性。然后,该任务实际上由 org.apache.spark.storage 包中的几个类处理:首先,BlockManager 只管理要持久化的数据块以及如何做到这一点的策略,将实际持久性委托给 DiskStore (当然是在磁盘上写入时),它代表一个用于写入的高级接口,而DiskBlockManager 则用于更多低级操作。
希望您了解现在该往哪里看,这样我们就可以继续前进并了解数据实际保存在哪里以及我们如何配置它:DiskBlockManager 调用了帮助程序 Utils.getConfiguredLocalDirs,这是为了实用我将在这里复制(取自链接的 2.2.1 版本,在撰写本文时的最新版本):
def getConfiguredLocalDirs(conf: SparkConf): Array[String] = {
val shuffleServiceEnabled = conf.getBoolean("spark.shuffle.service.enabled", false)
if (isRunningInYarnContainer(conf)) {
// If we are in yarn mode, systems can have different disk layouts so we must set it
// to what Yarn on this system said was available. Note this assumes that Yarn has
// created the directories already, and that they are secured so that only the
// user has access to them.
getYarnLocalDirs(conf).split(",")
} else if (conf.getenv("SPARK_EXECUTOR_DIRS") != null) {
conf.getenv("SPARK_EXECUTOR_DIRS").split(File.pathSeparator)
} else if (conf.getenv("SPARK_LOCAL_DIRS") != null) {
conf.getenv("SPARK_LOCAL_DIRS").split(",")
} else if (conf.getenv("MESOS_DIRECTORY") != null && !shuffleServiceEnabled) {
// Mesos already creates a directory per Mesos task. Spark should use that directory
// instead so all temporary files are automatically cleaned up when the Mesos task ends.
// Note that we don't want this if the shuffle service is enabled because we want to
// continue to serve shuffle files after the executors that wrote them have already exited.
Array(conf.getenv("MESOS_DIRECTORY"))
} else {
if (conf.getenv("MESOS_DIRECTORY") != null && shuffleServiceEnabled) {
logInfo("MESOS_DIRECTORY available but not using provided Mesos sandbox because " +
"spark.shuffle.service.enabled is enabled.")
}
// In non-Yarn mode (or for the driver in yarn-client mode), we cannot trust the user
// configuration to point to a secure directory. So create a subdirectory with restricted
// permissions under each listed directory.
conf.get("spark.local.dir", System.getProperty("java.io.tmpdir")).split(",")
}
}
我相信,该代码非常不言自明,并且注释很好(并且与文档的内容完全匹配):在 Yarn 上运行时,有一个依赖于 Yarn 容器存储的特定策略,在 Mesos 中它要么使用 Mesos 沙箱(除非启用 shuffle 服务),在所有其他情况下,它将转到spark.local.dir 或java.io.tmpdir 下设置的位置(可能是/tmp/)。
所以,如果你只是在玩,数据最有可能存储在/tmp/ 下,否则很大程度上取决于你的环境和配置。