我是scala的新手,准备了下面的function.In函数,我需要检查特定的路径是否存在:
def insertData(df: DataFrame, path: String): Unit = {
import sc.sqlContext.implicits._
// get output path to export the parquet files
val MPath = path+"/output"
var getDates = df.select("dates").distinct().collect().map(_(0)).toList
var invalidDates = new ListBuffer[String]()
// check if output path is present or not
if (new JFile(MPath ).exists) {
for (dates <- getDates) {
if (new JFile(MPath +"/Date=" + dates).exists) {
invalidDates += "Date=" + dates.toString
FileUtils.deleteDirectory(new JFile(MPath+ "/Date=" + dates))
}
else
{
log.info(s"No parquet associated with Date")
}
}
}
else
{
new JFile(MPath).mkdirs()
}
}在这里,我使用了new JFile(MPath ),然后在for循环中使用new JFile(MPath +"/Date=" + dates).exists).I进行连接,只需避免重写new JFile .Is就可以避免这种情况的发生
发布于 2019-08-04 22:27:47
使用Hadoop的FileSystem:
def doesPathExist(pathStr: String):Boolean = {
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
val path = new Path(pathStr)
val fileSystem = path.getFileSystem(new Configuration)
fileSystem.exists(path)
}https://stackoverflow.com/questions/57347007
复制相似问题