java—从hdfs复制文件时,文件保存到s3中的错误目录

v6ylcynt  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(229)

我编写了一个将文件从hdfs目录保存到s3的方法。但是这些文件被保存到了s3中错误的目录中。我检查了日志,确认 s3TargetPaths3://bucketName/data 以及 hdfsSource 也可以正确解析。
但不是被保存到 s3TargetPath 它们被保存到 s3://bucketName//data/ .
还有 s3://bucketName//data/ 目录包含一个文件 data 具有内容类型: binary/octet-stream ,fs类型: Hadoop block . 要将文件保存到正确的s3路径,我的代码中需要更改什么?

private String hdfsPath = "hdfs://localhost:9010/user/usr1/data";
private String s3Path = "s3://bucketName/data";

copyFromHdfstoS3(hdfsPath, s3Path);

//

void copyFromHdfstoS3(String hdfsDir, String s3sDir) {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(new URI(hdfsDir), conf);
FileSystem s3Fs = FileSystem.get(new URI(s3sDir), conf);

Path hdfsSource = new Path(hdfsDir);
Path s3TargetPath = new Path(s3sDir);

RemoteIterator<LocatedFileStatus> sourceFiles = hdfs.listFiles(sourcePath, false);

        if (!s3Fs.exists(s3TargetPath)) {
            s3Fs.mkdirs(s3TargetPath);
        }

        if (sourceFiles != null) {
            while (sourceFiles.hasNext()) {
                Path srcFilePath = sourceFiles.next().getPath();
                if (FileUtil.copy(hdfs, srcFilePath, s3Fs, s3TargetPath, false, true, new Configuration())) {
                    LOG.info("Copied Successfully");
                } else {
                    LOG.info("Copy Failed");
                }
            }
        }    
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题