Copying file from HDFS to Local Machine

前端 未结 2 962
栀梦
栀梦 2021-02-03 12:22

I\'m having a problem with trying to \"download\" file from HDFS file system to my local system. (even though opposite operation works without a problem). *Note: File exists on

相关标签:
2条回答
  • 2021-02-03 12:24

    Try using this method from the API :

    //where delSrc is do you want to delete the source, src and dst you already have and useRawLocalFileSystem should be set to true in your case
    hdfsFileSystem.copyToLocalFile(delSrc, src, dst, useRawLocalFileSystem);
    

    in your case replace the :

    hdfsFileSystem.copyToLocalFile(hdfs, local);
    

    with:

    hdfsFileSystem.copyToLocalFile(false, hdfs, local, true);
    
    0 讨论(0)
  • 2021-02-03 12:40

    You can follow code shown below:

    public static void main(String args[]){
        try {
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://localhost:54310/user/hadoop/");
            FileSystem fs = FileSystem.get(conf);
            FileStatus[] status = fs.listStatus(new Path("hdfsdirectory"));
            for(int i=0;i<status.length;i++){
                System.out.println(status[i].getPath());
                fs.copyToLocalFile(false, status[i].getPath(), new Path("localdir"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题