我正在尝试使用以下代码从 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);