简体   繁体   English

将本地目录的内容复制到hdfs中的目录

[英]copy the contents of a local directory to a directory in hdfs

I have got a requirement that I should be able to copy the contents of a directory from local system to a directory on HDFS. 我有一个要求,我应该能够将目录的内容从本地系统复制到HDFS上的目录。

The condition is only the directory contents should be copied to the location I have specified, not the source directory itself. 条件是仅目录内容应复制到我指定的位置,而不是源目录本身。 Using command copyfromlocal I can achieve this. 使用命令copyfromlocal可以实现此目的。 But I need to use Java. 但是我需要使用Java。 There is this method copyFromLocalFile which should be used for making a copy from local file system, the problem is it copies the directory itself. 有一个方法copyFromLocalFile应该用于从本地文件系统进行复制,问题是它复制目录本身。 Also tried using FileUtils.copy method, gives the same result as the copyFromLocalFile 还尝试使用FileUtils.copy方法,得到与copyFromLocalFile相同的结果

As a test I tried to copy directory contents from a test directory to another directory , both on the local file system. 作为测试,我尝试将目录内容从测试目录复制到本地文件系统上的另一个目录。 I used FileUtils.copyDirectory . 我使用FileUtils.copyDirectory This works but I cannot use it for HDFS. 这可行,但是我不能将其用于HDFS。 I have seen many links to related to this same question but could not find any way. 我已经看到许多与此问题相关的链接,但找不到任何方法。

Guys, could you please let me know if this is possible or not or is it some design flaw? 伙计们,能否请您告诉我这是否可行,或者是否存在设计缺陷? If this is possible how can I proceed ? 如果可能的话,我该如何进行?

Ya it's really hard to get things done with FileSystem api exactly the way you want. 是的,使用FileSystem api完全按照您想要的方式完成工作确实很困难。 Insufficient documentation makes things even harder(Too many methods, little explanation). 文档不足会使事情变得更加困难(方法太多,解释很少)。 I faced the same problem an year ago. 我一年前也遇到过同样的问题。

The only solution I got is iterative : 我得到的唯一解决方案是迭代:

void copyFilesToDir(String srcDirPath, FileSystem fs, String destDirPath)
        throws IOException {
    File dir = new File(srcDirPath);
    for (File file : dir.listFiles()) {
        fs.copyFromLocalFile(new Path(file.getPath()),
                new Path(destDirPath, file.getName()));
    }
}

I think the code needs little explanation. 我认为代码几乎不需要解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM