use of oi.thekraken.grok.api.Match in project graylog2-server by Graylog2.
the class GrokExtractor method run.
@Override
protected Result[] run(String value) {
// the extractor instance is rebuilt every second anyway
final Match match = grok.match(value);
match.captures();
final Map<String, Object> matches = match.toMap();
final List<Result> results = Lists.newArrayListWithCapacity(matches.size());
for (final Map.Entry<String, Object> entry : matches.entrySet()) {
// never add null values to the results, those don't make sense for us
if (entry.getValue() != null) {
results.add(new Result(entry.getValue(), entry.getKey(), -1, -1));
}
}
return results.toArray(new Result[results.size()]);
}
use of oi.thekraken.grok.api.Match 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 Grok grok = new Grok();
for (GrokPattern grokPattern : grokPatterns) {
grok.addPattern(grokPattern.name(), grokPattern.pattern());
}
grok.compile(pattern, namedCapturesOnly);
final Match match = grok.match(string);
match.captures();
final Map<String, Object> matches = match.toMap();
final GrokTesterResponse response;
if (matches.isEmpty()) {
response = GrokTesterResponse.create(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.create(true, responseMatches, pattern, string);
}
return response;
}
use of oi.thekraken.grok.api.Match in project cdap by caskdata.
the class GrokRecordFormat method read.
@Override
public StructuredRecord read(StreamEvent event) throws UnexpectedFormatException {
String bodyAsStr = Bytes.toString(event.getBody(), Charsets.UTF_8);
StructuredRecord.Builder builder = StructuredRecord.builder(schema);
Match gm = grok.match(bodyAsStr);
gm.captures();
Map<String, Object> x = gm.toMap();
for (Schema.Field field : schema.getFields()) {
String fieldName = field.getName();
Object value = x.get(fieldName);
if (value != null) {
builder.convertAndSet(fieldName, value.toString());
}
}
return builder.build();
}
Aggregations