NIFI-4702: When we check the next line for matches in Grok Reader, store the Map that is returned so that we don't have to re-evaluate the regexes the next time that nextRecord() is called

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2349
This commit is contained in:
Mark Payne 2017-12-18 09:29:52 -05:00 committed by Matthew Burgess
parent f50a07ec88
commit 406db4867a
1 changed files with 10 additions and 1 deletions

View File

@ -48,6 +48,7 @@ public class GrokRecordReader implements RecordReader {
private RecordSchema schema; private RecordSchema schema;
private String nextLine; private String nextLine;
Map<String, Object> nextMap = null;
static final String STACK_TRACE_COLUMN_NAME = "stackTrace"; static final String STACK_TRACE_COLUMN_NAME = "stackTrace";
static final String RAW_MESSAGE_NAME = "_raw"; static final String RAW_MESSAGE_NAME = "_raw";
@ -74,10 +75,13 @@ public class GrokRecordReader implements RecordReader {
@Override @Override
public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException {
Map<String, Object> valueMap = null; Map<String, Object> valueMap = nextMap;
nextMap = null;
StringBuilder raw = new StringBuilder(); StringBuilder raw = new StringBuilder();
int iterations = 0;
while (valueMap == null || valueMap.isEmpty()) { while (valueMap == null || valueMap.isEmpty()) {
iterations++;
final String line = nextLine == null ? reader.readLine() : nextLine; final String line = nextLine == null ? reader.readLine() : nextLine;
raw.append(line); raw.append(line);
nextLine = null; // ensure that we don't process nextLine again nextLine = null; // ensure that we don't process nextLine again
@ -90,6 +94,10 @@ public class GrokRecordReader implements RecordReader {
valueMap = match.toMap(); valueMap = match.toMap();
} }
if (iterations == 0 && nextLine != null) {
raw.append(nextLine);
}
// Read the next line to see if it matches the pattern (in which case we will simply leave it for // Read the next line to see if it matches the pattern (in which case we will simply leave it for
// the next call to nextRecord()) or we will attach it to the previously read record. // the next call to nextRecord()) or we will attach it to the previously read record.
String stackTrace = null; String stackTrace = null;
@ -111,6 +119,7 @@ public class GrokRecordReader implements RecordReader {
} }
} else { } else {
// The next line matched our pattern. // The next line matched our pattern.
nextMap = nextValueMap;
break; break;
} }
} }