ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • java grok pattern
    Tech/SpringBoot 2015. 10. 12. 13:59
    반응형


     파일에서 line by line 으로 문자열 리스트를 읽어온 후 해당 문자열 리스트들을 Grok 패턴의 정의에 따라 문자열 리스트 맵을 구성 할 수있는 함수이다.


    public List<Map<String,String>> makeResultList(List<String> strList, String expression)throws Exception 

    {

    List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();

    final GrokDictionary dictionary = new GrokDictionary();

    dictionary.addBuiltInDictionaries();

    dictionary.bind();

    Grok compiledPattern = dictionary.compileExpression(expression);

    if(strList !=null && !expression.isEmpty())

    {

    for (String str : strList) 

    {

    Map<String,String> strMap = null;

    try

    {

    strMap = compiledPattern.extractNamedGroups(str);

    if(strMap != null)

    {

    resultList.add(strMap);

    }

    }

    catch(Exception e)

    {

    System.out.println(e+"\n"+str);

    throw new Exception(e);

    }

    }

    }

    return resultList;

    }


    사용법:

    final String strTmp= "1234567 - israel.ekpo@massivelogdata.net none 1789 Hello Grok";

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

    final GrokDictionary dictionary = new GrokDictionary();

    dictionary.addBuiltInDictionaries();

    dictionary.bind();


        List<String> testList = new ArrayList<String>();

        testList.add(strTmp);

    List<Map<String,String>> list = new ArrayList<Map<String,String>>();

    list = makeResultList(testList, expression);


    출처: http://stackoverflow.com/questions/19565755/how-to-parse-using-grok-from-java-is-there-any-example-available



    반응형
Designed by Tistory.