我正在尝试在两个s3存储桶之间复制文件。
我就是这样做到的:
val hadoopConfiguration = spark.sparkContext.hadoopConfiguration
val srcS3Bucket = "s3://src_bucket"
val rawSrcPath = "s3://src_bucket/output/some_prefix/some_more_prefix/"
val rawDestPath = "s3://dest_bucket/from-source/"
val srcPath = new Path(rawSrcPath)
val srcFs = FileSystem.get(
srcPath.toUri, hadoopConfiguration)
val dstPath: Path = new Path(rawDestPath)
val dstFs = FileSystem.get(
dstPath.toUri, hadoopConfiguration)
val fileSystem = dstPath.getFileSystem(hadoopConfiguration)
val paths: ListBuffer[Path] = ListBuffer()
val fileStatusListIterator: RemoteIterator[LocatedFileStatus] = srcFs.listFiles(srcPath, true)
while (fileStatusListIterator.hasNext) {
val fileStatus: LocatedFileStatus = fileStatusListIterator.next()
println(fileStatus.getPath)
paths += fileStatus.getPath
}
// Destination location cleanup before write
dstFs.delete(dstPath, true)
var df_list: ListBuffer[DataFrame] = ListBuffer()
paths.foreach { path =>
val partitioName=path.toUri.getRawPath.toString.replace("/output/some_prefix/some_more_prefix/","")
val finalPath = rawDestPath + partitioName
df_list += Seq(finalPath).toDF("filename")
printf("Copying to: %s ", finalPath)
val deleteSource = false
FileUtil.copy(srcFs, path, dstFs, new Path(finalPath), deleteSource, hadoopConfiguration)
}
我用多种场景测试了代码,比如只传输文件,在子目录下传输文件等,代码运行良好。
然而,我面临的问题,而它是试图复制 .tmp
文件夹。 srcFs.listFiles(srcPath, true)
列出了几个 .tmp
文件,以及其他文件和子目录。但在执行时 FileUtil.copy
,它抛出了一个错误,说
“不存在这样的<file\u name>.tmp文件”
对如何解决这个问题有什么建议吗?
编辑:
我做了一个跳过那些文件的变通方法 .tmp
. 不过,我还是想了解一下是怎么回事 Filesystem.list
列出一个不存在的文件。
暂无答案!
目前还没有任何答案,快来回答吧!