0

https://github.com/thekrakken/java-grok

I'm using this Grok API for Java. Code is as follows:

    Grok grok = Grok.EMPTY;

    // add a pattern to grok
    grok.addPatternFromFile("pat.txt");

    // compile and add semantic
    grok.compile("%{NUMBER:hits} %{USER:word}");

            String str = "234 wdfd\n";
    Match m = grok.match(str);
    m.captures();

    // Print
    System.out.println(m.toJson());


    grok = Grok.EMPTY;
    str = "ssdfsdf\n";
    Match m2 = grok.match(str);
    m2.captures();

    System.out.println(m2.toJson());

The output of the program is:

            {"hits":234,"word":"wdfd"}
            {"hits":234,"word":"wdfd"}

I emptied the Grok instance, I used a separate "m2" match variable, but still the program returns the last successful match instead of NULL or an error that can inform me that the match has failed. How can I know when a match fails?

1 Answers1

0

It was actually a little bug (due to a poc), I fixed the problem.

Now the unmached row will return empty.

example:

Grok grok = Grok.create("pat.txt");
// compile and add semantic
grok.compile("%{NUMBER:hits} %{USER:word}");

String str = "234 wdfd\n";
Match m = grok.match(str);
m.captures();

// Print
System.out.println(m.toJson());


// Here you dont need to create a new instance of grok [grok = Grok.EMPTY;]
str = "ssdfsdf\n";
// here you can reuse the matcher if you want.
Match m2 = grok.match(str);
m2.captures();

System.out.println(m2.toJson());

The output of the program now is:

{"hits":234,"word":"wdfd"}
{}

hope its help

AnthonyC
  • 277
  • 7
  • 16