0

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

我正在使用这个 Grok API for Java。代码如下:

    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());

程序的输出是:

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

我清空了 Grok 实例,我使用了一个单独的“m2”匹配变量,但程序仍然返回最后一次成功的匹配而不是 NULL 或可以通知我匹配失败的错误。我怎么知道匹配失败?

4

1 回答 1

0

这实际上是一个小错误(由于 poc),我解决了这个问题。

现在未加工的行将返回空。

例子:

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());

现在程序的输出是:

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

希望它的帮助

于 2014-06-07T05:05:41.647 回答