6

我正在尝试使用 Java API 访问 HDFS 中的文件,但每次我得到文件未找到。我用来访问的代码是:-

Configuration conf = new Configuration();
    conf.addResource(FileUtilConstants.ENV_HADOOP_HOME + FileUtilConstants.REL_PATH_CORE_SITE);
    conf.addResource(FileUtilConstants.ENV_HADOOP_HOME + FileUtilConstants.REL_PATH_HDFS_SITE);

    try {
        FileSystem fs = FileSystem.get(conf);
        Path hdfsfilePath = new Path(hdfsPath);
        logger.info("Filesystem URI : " + fs.getUri());
        logger.info("Filesystem Home Directory : " + fs.getHomeDirectory());
        logger.info("Filesystem Working Directory : " + fs.getWorkingDirectory());
        logger.info("HDFS File Path : " + hdfsfilePath);
        if (!fs.exists(hdfsfilePath)) {
            logger.error("File does not exists : " + hdfsPath);
        }

这是代码的命令行输出。

[root@koversevms ~]# java -jar /tmp/thetus-incendiary-koverse-extension-fileutils-1.0-SNAPSHOT.jar 
13/07/10 02:47:18 INFO fileutils.HadoopFileChecksumUtils: Filesystem URI : file:///
13/07/10 02:47:18 INFO fileutils.HadoopFileChecksumUtils: Filesystem Home Directory : file:/root
13/07/10 02:47:18 INFO fileutils.HadoopFileChecksumUtils: Filesystem Working Directory : file:/root
13/07/10 02:47:18 INFO fileutils.HadoopFileChecksumUtils: HDFS File Path : /usr/hadoop/sample/sample.txt
13/07/10 02:47:18 ERROR fileutils.HadoopFileChecksumUtils: File does not exists : /usr/hadoop/sample/sample.txt

我是hadoop的新手,所以我不知道出了什么问题。

4

1 回答 1

11

这是最初在回答这个问题的上下文中发布的代码片段。尽管原始问题的意图不同,它也应该解决您的问题。您的代码中的要点是您从方案 ( file://) 开始遇到问题。请检查fs.defaultFS您的配置中的变量。

package org.myorg;

import java.security.PrivilegedExceptionAction;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;

public class HdfsTest {

    public static void main(String args[]) {

        try {
            UserGroupInformation ugi
                = UserGroupInformation.createRemoteUser("hbase");

            ugi.doAs(new PrivilegedExceptionAction<Void>() {

                public Void run() throws Exception {

                    Configuration conf = new Configuration();
                    conf.set("fs.defaultFS", "hdfs://1.2.3.4:8020/user/hbase");
                    conf.set("hadoop.job.ugi", "hbase");

                    FileSystem fs = FileSystem.get(conf);

                    fs.createNewFile(new Path("/user/hbase/test"));

                    FileStatus[] status = fs.listStatus(new Path("/user/hbase"));
                    for(int i=0;i<status.length;i++){
                        System.out.println(status[i].getPath());
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
于 2013-07-11T08:19:23.697 回答