2013-10-24 15 views

答えて

4

このJavaライブラリをチェック

https://github.com/aicer/grok

あなたのように、プロジェクトに含めることができます依存関係

<dependency> 
    <groupId>org.aicer.grok</groupId> 
    <artifactId>grok</artifactId> 
    <version>0.9.0</version> 
</dependency> 

定義済みのパターンを追加することもできます。

名前付きパターンが抽出され、グループ名をキーとして検索結果がマップに表示され、検索された値がこれらのキーにマップされます。

final GrokDictionary dictionary = new GrokDictionary(); 

// Load the built-in dictionaries 
dictionary.addBuiltInDictionaries(); 

// Add custom pattern 
dictionary.addDictionary(new File(patternDirectoryOrFilePath)); 

// Resolve all expressions loaded 
dictionary.bind(); 

この次の例では、ここでファイル

final GrokDictionary dictionary = new GrokDictionary(); 

// Load the built-in dictionaries 
dictionary.addBuiltInDictionaries(); 

// Add custom pattern directly 

dictionary.addDictionary(new StringReader("DOMAINTLD [a-zA-Z]+")); 
dictionary.addDictionary(new StringReader("EMAIL %{NOTSPACE}@%{WORD}\.%{DOMAINTLD}")); 

// Resolve all expressions loaded 
dictionary.bind(); 

を使用しなくても辞書に直接文字列パターンを追加し、ライブラリに

public final class GrokStage { 

    private static final void displayResults(final Map<String, String> results) { 
    if (results != null) { 
     for(Map.Entry<String, String> entry : results.entrySet()) { 
     System.out.println(entry.getKey() + "=" + entry.getValue()); 
     } 
    } 
    } 

    public static void main(String[] args) { 

    final String rawDataLine1 = "1234567 - [email protected] cc55ZZ35 1789 Hello Grok"; 
    final String rawDataLine2 = "98AA541 - [email protected] mmddgg22 8800 Hello Grok"; 
    final String rawDataLine3 = "55BB778 - [email protected] secret123 4439 Valid Data Stream"; 

    final String expression = "%{EMAIL:username} %{USERNAME:password} %{INT:yearOfBirth}"; 

    final GrokDictionary dictionary = new GrokDictionary(); 

    // Load the built-in dictionaries 
    dictionary.addBuiltInDictionaries(); 

    // Resolve all expressions loaded 
    dictionary.bind(); 

    // Take a look at how many expressions have been loaded 
    System.out.println("Dictionary Size: " + dictionary.getDictionarySize()); 

    Grok compiledPattern = dictionary.compileExpression(expression); 

    displayResults(compiledPattern.extractNamedGroups(rawDataLine1)); 
    displayResults(compiledPattern.extractNamedGroups(rawDataLine2)); 
    displayResults(compiledPattern.extractNamedGroups(rawDataLine3)); 
    } 
} 
を使用する方法の完全な例である
関連する問題