我在集群上运行该程序时遇到了问题,因此决定在函数 map 和 reduce 中读取 hdfs 文件。如何逐行读取hdfs文件并烧录到ArrayList中读取行?
最佳答案
只是演示的代码片段:
Path path = new Path(filePath);
FileSystem fs = path.getFileSystem(context.getConfiguration()); // context of mapper or reducer
FSDataInputStream fdsis = fs.open(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fdsis));
String line = "";
ArrayList<String> lines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();
关于java - 在 Java hdfs 中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13166123/