无法从hadoop hdfs检索文件

osh3o9ms  于 2021-06-03  发布在  Hadoop
关注(0)|答案(3)|浏览(304)

我正在学习如何从hdfs读写文件。
这是我用来阅读的代码:

import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class FileSystemCat {
public static void main (String [] args) throws Exception {

    String uri = "/user/hadoop/file.txt";
    Configuration conf = new Configuration();
    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/core-site.xml"));
    conf.addResource(new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml"));

    FileSystem fs = FileSystem.get(URI.create(uri),conf);

    InputStream in = null;
    try{

        in = fs.open(new Path(uri));
        IOUtils.copyBytes(in, System.out, 4096,false);
    }finally{
        IOUtils.closeStream(in);
    }           
}

}
文件在那里

然而,当我在eclipse中运行代码时,我得到了以下信息

Exception in thread "main" java.io.FileNotFoundException: File /user/hadoop/file.txt does not exist
at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:511)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:724)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:501)
at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:397)
at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.<init>(ChecksumFileSystem.java:137)
at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:339)
at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:764)
at hadoop.FileSystemCat.main(FileSystemCat.java:22)

我使用file:///user/hadoop/file.txt和hdfs:///user/hadoop/file.txt作为路径
对于后者,误差略有不同:

Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs

core-site.xml文件

<configuration>
   <property>
     <name>fs.default.name</name>
     <value>hdfs://localhost/</value>
   </property>
</configuration>

hdfs-site.xml文件

<configuration>
<property>
   <name>dfs.replication</name>
   <value>2</value>
 </property>

 <property>
   <name>dfs.namenode.name.dir</name>
   <value>file:///usr/local/hadoop_store/hdfs/namenode/</value>
 </property>

 <property>
   <name>dfs.datanode.data.dir</name>
   <value>file:///usr/local/hadoop_store/hdfs/datanode/,file:///mnt/hadoop/hadoop_store/hdfs/datanode/</value>
 </property>

 <property>
   <name>dfs.webhdfs.enabled</name>
   <value>true</value>
 </property>
</configuration>

有什么问题吗?
谢谢

rjjhvcjd

rjjhvcjd1#

如果您想读取hdfs文件中的数据,那么这段代码可以做到这一点。

package com.yp.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class ReadHadoopFileData {

public static void main(String[] args) throws IOException {

    Configuration conf = new Configuration();
    FileSystem hdfs = FileSystem.get(conf);

    Path hdfsFile = new Path(args[0]);

    try {
        BufferedReader br=new BufferedReader(new InputStreamReader(hdfs.open(hdfsFile)));
        String line;
        line=br.readLine();
        while (line != null){
                System.out.println(line);
                line=br.readLine();
        }

    }catch (IOException ioe) {
        ioe.printStackTrace();
    }   
  }

}

当您使用命令行运行时,hadoop将负责所有的环境设置。
运行上述程序的命令(假设您创建了read.jar,hdfs文件是part-r-00000)

hadoop jar Read.jar com.yp.util.ReadHadoopFileData /MyData/part-r-00000
r9f1avp5

r9f1avp52#

你应该换条线

FileSystem fs = FileSystem.get(URI.create(uri),conf);

像这样的事情

FileSystem fs = FileSystem.get(URI.create("hdfs://localhost"), conf);

如果您的uri路径在hdfs中,那么这应该是可行的。
要查看uri路径是否在hdfs中,可以 hadoop fs -ls / 在命令行中

qhhrdooz

qhhrdooz3#

添加带有hdfs配置参数的xml文件:

Configuration conf = new Configuration();
conf.addResource(new Path("your_hadoop_path/conf/core-site.xml"));
conf.addResource(new Path("your_hadoop_path/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(URI.create(uri),conf);

相关问题