Sollicitatievraag

Sollicitatiegesprek voor de functie Software Developer

-Amsterdam

Booking.com

We have a log file, can grow pretty big. Each line is a trace-log, and the first field is the RequestID. We need to scan the file, and print all the logs for requests which resulted in error .. 001 <timestamp> BEGIN 001 <timestamp> fetched from db 001 <timestamp> some processing .. 002 <timestamp> BEGIN .. 002 <timestamp> fetched from db 001 <timestamp> returned success 003 <timestamp> BEGIN 001 <timestamp> END 003 <timestamp> some work 002 <timestamp> ERROR 003 <timestamp> some other work

Antwoord

Antwoorden op sollicitatievragen

5 antwoorden

2

In Java we can use Scanner object and its findInLine method to search using regexp patterns

Arman op

5

public static void print(String fileName) throws IOException { RandomAccessFile randomFile = new RandomAccessFile(fileName, "r"); long position=randomFile.length()-1; int thisCode; char thisChar; String thisLine; String reqId = ""; while (position >= 0){ randomFile.seek(position); thisCode = randomFile.readByte(); thisChar = (char) thisCode; if (thisChar == 10 || position==0) { if (position == 0) randomFile.seek(position); thisLine = randomFile.readLine(); if (thisLine != null) { int i = thisLine.indexOf(" "); if (i>0) { String id = thisLine.substring(0, i); if (thisLine.contains("ERROR") || id.equalsIgnoreCase(reqId)) { reqId = id; System.out.println(thisLine); } } } } position--; } }

Ebru Kanat op

0

awk: /ERROR/{ print buffer[$1],$0 } /END/{ delete buffer[$1] } //{ buffer[$1] = buffer[$1],$0 }

sasa op

0

actualy in prev solution /END/ should be at the end

sasa op

0

Files.lines(new File("Log.txt").toPath()).filter(f->f.contains("Error")).collect(Collectors.toList());

Anoniem op

Voeg antwoorden of opmerkingen toe

Meld u aan of registreer u om hier een opmerking over te maken.