将文件从HDFS复制到本地计算机 [英] Copying file from HDFS to Local Machine

查看:330
本文介绍了将文件从HDFS复制到本地计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从HDFS文件系统下载文件到我的本地系统时遇到问题。 (即使相反的操作没有问题)。
*注意:文件存在于HDFS文件系统的指定路径上



以下是一段代码片段:

  Configuration conf = new Configuration(); 
conf.set(fs.defaultFS,$ {NAMENODE_URI});
FileSystem hdfsFileSystem = FileSystem.get(conf);

String result =;

Path local = new Path($ {SOME_LOCAL_PATH});
路径hdfs =新路径($ {SOME_HDFS_PATH});

String fileName = hdfs.getName();

if(hdfsFileSystem.exists(hdfs))
{
hdfsFileSystem.copyToLocalFile(hdfs,local);
result =文件+ fileName +复制到本地计算机的位置:+ localPath;
}
else
{
result =File+ fileName +在HDFS上不存在位置:+ localPath;
}

返回结果;

例外情况如下:

  12/07/13 14:57:46 WARN util.NativeCodeLoader:无法为您的平台加载native-hadoop库......在适用的情况下使用builtin-java类
Exception在线程main中java.io.IOException:无法运行程序cygpath:CreateProcess error = 2,系统无法在java.lang.ProcessBuilder.start(未知源)找到指定的
文件
在org.apache.hadoop.util.Shell.runCommand(Shell.java:206)
在org.apache.hadoop.util.Shell.run(Shell.java:188)
在org.apache .hadoop.fs.FileUtil $ CygPathCommand。< init>(FileUtil.java:412)
at org.apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:438)
at org。 apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:465)
位于org.apache.hadoop.fs.RawLocalFileSystem.execCommand(RawLocalFileSystem.java:573)
位于org.apache.hadoop。 fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.jav a:565)
at org.apache.hadoop.fs.FilterFileSystem.setPermission(FilterFileSystem.java:403)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:452)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:420)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:774)
在org.apache.hadoop.fs.FileSystem.create(FileSystem.java:755)
在org.apache.hadoop.fs.FileSystem.create(FileSystem.java:654)
在org.apache .hadoop.fs.FileUtil.copy(FileUtil.java:259)
at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:232)
at org.apache.hadoop.fs .FileUtil.copy(FileUtil.java:183)
at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:1837)
at org.apache.hadoop.fs.FileSystem.copyToLocalFile (FileSystem.java:1806)
at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:1782)
at com.hmeter.hadoop.hdfs.hdfsoperations.HdfsOperations.f ileCopyFromHdfsToLocal(HdfsOperations.java:75)
at com.hmeter.hadoop.hdfs.hdfsoperations.HdfsOperations.main(HdfsOperations.java:148)
引起:java.io.IOException:CreateProcess error = 2 ,系统无法在java.lang.ProcessImpl.create(Native Method)
处找到指定
的文件(位于java.lang.ProcessImpl)。< init>(Unknown Source)
at java。 lang.ProcessImpl.start(Unknown Source)
... 22 more

可能是一个问题?为什么它需要Cygwin的cyqpath?我在Windows 7上运行这个代码。



谢谢

尝试从API中使用此方法:

  //其中delSrc是要删除源,src和dst你已经有和useRawLocalFileSystem应设置为true在您的情况
hdfsFileSystem.copyToLocalFile(delSrc,src,dst,useRawLocalFileSystem);

在您的案例中替换:

  hdfsFileSystem.copyToLocalFile(hdfs,local); 

附带:

  hdfsFileSystem.copyToLocalFile(false,hdfs,local,true); 


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 the HDFS file system on specified path

Here is a code snippet:

    Configuration conf = new Configuration();
    conf.set("fs.defaultFS", "${NAMENODE_URI}");
    FileSystem hdfsFileSystem = FileSystem.get(conf);

    String result = "";

    Path local = new Path("${SOME_LOCAL_PATH}");
    Path hdfs = new Path("${SOME_HDFS_PATH}");

    String fileName = hdfs.getName();

    if (hdfsFileSystem.exists(hdfs))
    {
        hdfsFileSystem.copyToLocalFile(hdfs, local);
        result = "File " + fileName + " copied to local machine on location: " + localPath;
    }
    else
    {
        result = "File " + fileName + " does not exist on HDFS on location: " + localPath;
    }

    return result;

Exception that I get is following:

12/07/13 14:57:46 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Exception in thread "main" java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.hadoop.util.Shell.runCommand(Shell.java:206)
    at org.apache.hadoop.util.Shell.run(Shell.java:188)
    at org.apache.hadoop.fs.FileUtil$CygPathCommand.<init>(FileUtil.java:412)
    at org.apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:438)
    at org.apache.hadoop.fs.FileUtil.makeShellPath(FileUtil.java:465)
    at org.apache.hadoop.fs.RawLocalFileSystem.execCommand(RawLocalFileSystem.java:573)
    at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:565)
    at org.apache.hadoop.fs.FilterFileSystem.setPermission(FilterFileSystem.java:403)
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:452)
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:420)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:774)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:755)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:654)
    at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:259)
    at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:232)
    at org.apache.hadoop.fs.FileUtil.copy(FileUtil.java:183)
    at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:1837)
    at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:1806)
    at org.apache.hadoop.fs.FileSystem.copyToLocalFile(FileSystem.java:1782)
    at com.hmeter.hadoop.hdfs.hdfsoperations.HdfsOperations.fileCopyFromHdfsToLocal(HdfsOperations.java:75)
    at com.hmeter.hadoop.hdfs.hdfsoperations.HdfsOperations.main(HdfsOperations.java:148)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 22 more

Any idea what could be an issue? Why it is requiring the cyqpath for Cygwin? I'm running this code on Windows 7.

Thanks

解决方案

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);

这篇关于将文件从HDFS复制到本地计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆