我正在开发Hadoop MapReduce。我已经创建了MapReduce程序,该程序为我们的大型Apache日志进行了反向索引。 MapReduce的输出如下所示:
{"Error" {log1 {200,300,500}, log2{400,900,800..}}
{"201" {log5 {250,350,550}, log2{850,950,80..}}
{"400" {log3 {280,380,580}, log2{480,980,880..}}
在这里,我将存储带有单词倒排索引值的单词,以及它在名称中出现在日志文件中的位置。
现在,我想编写示例脚本/程序,通过它我可以按行号搜索日志。我将在MarPreduce输出上方使用。我该如何通过Java进行操作?
示例:如果用户在Apache日志中搜索错误,我将使用log1,log2并显示200,300,400行中的记录
最佳答案
BufferedReader reader;
int lineCount = 0;
int lineYouWant = 100;
reader = new BufferedReader(new InputStreamReader(new FileInputStream("sample.txt")));
while (reader.ready()) {
String line = reader.readLine();
lineCount++;
if (lineCount == lineYouWant) doStuff();
使用以上内容作为起点。您必须读入整个文件(直到找到所需行的位置),因为这是“计数”行数的唯一方法。
关于java - 如何在Java中按行号搜索文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19699304/