use of io.krakens.grok.api.GrokCompiler in project graylog2-server by Graylog2.
the class InMemoryGrokPatternService method match.
@Override
public Map<String, Object> match(GrokPattern pattern, String sampleData) throws GrokException {
final Set<GrokPattern> patterns = loadAll();
final GrokCompiler grokCompiler = GrokCompiler.newInstance();
for (GrokPattern storedPattern : patterns) {
grokCompiler.register(storedPattern.name(), storedPattern.pattern());
}
grokCompiler.register(pattern.name(), pattern.pattern());
Grok grok = grokCompiler.compile("%{" + pattern.name() + "}");
return grok.match(sampleData).captureFlattened();
}
use of io.krakens.grok.api.GrokCompiler in project graylog2-server by Graylog2.
the class GrokTesterResource method doTestGrok.
private GrokTesterResponse doTestGrok(String string, String pattern, boolean namedCapturesOnly) throws GrokException {
final Set<GrokPattern> grokPatterns = grokPatternService.loadAll();
final GrokCompiler grokCompiler = GrokCompiler.newInstance();
for (GrokPattern grokPattern : grokPatterns) {
grokCompiler.register(grokPattern.name(), grokPattern.pattern());
}
final Grok grok;
try {
grok = grokCompiler.compile(pattern, namedCapturesOnly);
} catch (Exception e) {
return GrokTesterResponse.createError(pattern, string, e.getMessage());
}
final Match match = grok.match(string);
final Map<String, Object> matches = match.captureFlattened();
final GrokTesterResponse response;
if (matches.isEmpty()) {
response = GrokTesterResponse.createSuccess(false, Collections.<GrokTesterResponse.Match>emptyList(), pattern, string);
} else {
final List<GrokTesterResponse.Match> responseMatches = Lists.newArrayList();
for (final Map.Entry<String, Object> entry : matches.entrySet()) {
final Object value = entry.getValue();
if (value != null) {
responseMatches.add(GrokTesterResponse.Match.create(entry.getKey(), value.toString()));
}
}
response = GrokTesterResponse.createSuccess(true, responseMatches, pattern, string);
}
return response;
}
Aggregations