gpt4 book ai didi

java - 如何使用 java 从 hdfs 读取多个文件?

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

我一直在研究 Hadoop,现在我正试图找出一种从目录中读取多个文件的方法。当我读取文件时,下面的代码工作正常。从 hdfs 读取多个文件并从每个文件读取每一行的最佳方法是什么。

try {
Path pt = new Path("hdfs://profile/generate/work/output/errors.txt");
FileSystem fs = FileSystem.get(job.getConfiguration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line;
line=br.readLine();
while (line !=null){
//sendemail
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

只需添加一个 FileStatus[] status = fs.listStatus(new Path(path)) 并在读取一个 hdfs 文件之前对您的状态进行 for

FileStatus[] status = fs.listStatus(new Path("path"));

for (int i=0;i<status.length;i++){

//afficher le contenu d'un fichier hdfs
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(status[i].getPath())));
String line;
line=br.readLine();
while (line != null){
System.out.println(line);
line=br.readLine();
}
}

关于java - 如何使用 java 从 hdfs 读取多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31922918/

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