Java将HDFS中的文件复制到HDFS中的另一个目录

jym*_*mbo 4 java hadoop hdfs

我使用的例子在这个环节在这里从一个目录复制HDFS内容在HDFS另一个目录。复制文件是可行的,但是与仅将文件复制到目标目录相比,它在目标中创建了一个新的子目录。例:

  Path source=new Path("hdfs://HANameService/sources/hpm_support/apc_code/");
  Path target=new Path("hdfs://HANameService/staging/hpm_support/apc_code/");
  FileSystem fs = source.getFileSystem(conf); 
  FileUtil.copy(fs, source, fs, target, true, conf);`
Run Code Online (Sandbox Code Playgroud)

因此,hdfs://HANameService/staging/hpm_support/apc_code与其将文件复制到该文件,而是在apc_code下创建一个新目录,该文件最终显示在hdfs://HANameService/staging/hpm_support/apc_code/apc_code如何获取而不创建该子目录的位置?

Ram*_*jan 6

你需要list的文件source directorycopy每个文件中使用iterator

            Path source=new Path("hdfs://HANameService/sources/hpm_support/apc_code/");
            Path target=new Path("hdfs://HANameService/staging/hpm_support/apc_code/");
            FileSystem fs = source.getFileSystem(conf);
            RemoteIterator<LocatedFileStatus> sourceFiles = fs.listFiles(source, true);
            if(sourceFiles != null) {
                while(sourceFiles.hasNext()){
                    FileUtil.copy(fs, sourceFiles.next().getPath(), fs, target, true, conf);
                }           
            }
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助