Ich möchte die Datei in das gleiche HDFS kopieren, genau wie Kopieren von HDFS: // abc: 9000/user/a.txt zu HDFS: // abc: 9000/user/123/Wie man Datei in HDFS mit JAVA API verschiebt oder kopiert
Kann ich das mit JAVA API? Danke
Ich möchte die Datei in das gleiche HDFS kopieren, genau wie Kopieren von HDFS: // abc: 9000/user/a.txt zu HDFS: // abc: 9000/user/123/Wie man Datei in HDFS mit JAVA API verschiebt oder kopiert
Kann ich das mit JAVA API? Danke
FileUtil bietet eine Methode zum Kopieren von Dateien.
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://abc:9000");
FileSystem filesystem = FileSystem.get(configuration);
FileUtil.copy(filesystem, new Path("src/path"), filesystem, new Path("dst/path"), false, configuration);
Wenn müssen Sie es auf einen anderen Cluster kopieren, machen gerade einen neuen Configuration
und Setup und neue FileSystem
.
Dateien zu "verschieben", anstatt sie zu kopieren, setzen Sie einfach den booleschen Parameter 'deleteSource' auf' true' – Sean
If you want to move files from directory it is little bit tricky below code done same task for me !!
val conf = new org.apache.hadoop.conf.Configuration()
val src:Path = new org.apache.hadoop.fs.Path(hdfsDirectory)
val fs = FileSystem.get(src.toUri,conf)
val srcPath: Path = new Path("hdfs://sourcePath/")
val srcFs =FileSystem.get(srcPath.toUri,conf)
val dstPath:Path =new Path("hdfs://targetPath/")
val dstFs =FileSystem.get(dstPath.toUri,conf)
val exists = fs.exists(new org.apache.hadoop.fs.Path(hdfsDirectory))
val status:Array[FileStatus] = fs.listStatus(new Path(hdfsDirectory))
if (status.length>0) {
status.foreach(x => {
println("My files: " + x.getPath)
FileUtil.copy(srcFs, x.getPath, dstFs, dstPath, true, conf)
println("Files moved !!" +x.getPath)
}
)}
else{
println("No Files Found !!")
}
Sie können in FileSystem API schauen. –