我是靠谱客的博主 直率羽毛,这篇文章主要介绍spark中 hdfs文件路径检测是否存在、删除hdfs路径,现在分享给大家,希望可以做个参考。

spark中,检测hdfs文件夹是否存在 和 删除文件

import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.ml.common.HdfsHelper.isDir

1、检测hdfs文件路径是否存在
def pathIsExist(spark: SparkSession, path: String): Boolean = {
//取文件系统
val filePath = new org.apache.hadoop.fs.Path( path )
val fileSystem = filePath.getFileSystem( spark.sparkContext.hadoopConfiguration )
// 判断路径是否存在
fileSystem.exists( filePath )
}

2、删除文件
def deleteFile(spark: SparkSession, path: String) : Boolean = {
val filePath = new org.apache.hadoop.fs.Path( path )
val fileSystem = filePath.getFileSystem( spark.sparkContext.hadoopConfiguration )
if (isDir(fileSystem, path))
fileSystem.delete(new Path(path), true)//true: delete files recursively
else
fileSystem.delete(new Path(path), false)
}

最后

以上就是直率羽毛最近收集整理的关于spark中 hdfs文件路径检测是否存在、删除hdfs路径的全部内容,更多相关spark中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部