-
java grok patternTech/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
반응형'Tech > SpringBoot' 카테고리의 다른 글
spring boot jpa(querydsl) (0) 2015.10.20 Spring data jpa cascade 관련 오류 (0) 2015.10.15 디렉터리의 특정패턴의 파일 리스트 가져오기 (0) 2015.10.12 spring boot 데몬 어플리케이션 만들기 (0) 2015.09.28 비트를 이용한 플래그 사용법 (0) 2015.02.02