gpt4 book ai didi

从 hdfs 读取文件时出现 FileNotFound 异常

转载 作者:可可西里 更新时间:2023-11-01 16:52:22 29 4
gpt4 key购买 nike

我正在尝试使用以下代码从 HDFS 读取文件:

final Configuration configuration = new Configuration();

configuration.set("fs.defaultFS", "hdfs://localhost:8020/user/training/");

FileSystem fileSystem = FileSystem.get(configuration);
String filePath = "hdfs" + "://" + "localhost:8020" + "/user/training/test.txt";

File fileToProcess = new File(filePath); // path of file
FileInputStream fis = new FileInputStream(fileToProcess);

程序的最后一条语句返回以下异常:

Exception in thread "main" java.io.FileNotFoundException:
hdfs:/localhost:8020/user/training/test.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)

知道可能有什么问题吗?文件存在于该位置。我能注意到的唯一区别是:虽然我在 URL 中放置了“//”,但最后一条语句在创建输入流时省略了一个斜线。

这段代码是从 MR Driver 类调用的。

最佳答案

Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:8020"),
configuration);
Path path = new Path("hdfs://localhost:8020/user/training/test.txt");
System.out.println(fs.exists(path));
FSDataInputStream fin = fs.open(path);

关于从 hdfs 读取文件时出现 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31898762/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com