oi.thekraken.grok.api.Grok.addPatternFromReader()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中oi.thekraken.grok.api.Grok.addPatternFromReader()方法的一些代码示例,展示了Grok.addPatternFromReader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Grok.addPatternFromReader()方法的具体详情如下:
包路径:oi.thekraken.grok.api.Grok
类名称:Grok
方法名:addPatternFromReader

Grok.addPatternFromReader介绍

暂无

代码示例

代码示例来源:origin: apache/metron

private static Grok getGrok(String grokExpr) throws GrokException {
 Grok grok = new Grok();
 InputStream input = GrokFunctions.class.getResourceAsStream("/patterns/common");
 if(input != null) {
  grok.addPatternFromReader(new InputStreamReader(input));
 }
 if(grokExpr != null) {
  grok.addPatternFromReader(new StringReader("pattern " + grokExpr));
  grok.compile("%{pattern}");
 }
 return grok;
}

代码示例来源:origin: apache/metron

@Bean
  public Grok commonGrok() throws GrokException {
    Grok grok = new Grok();
    grok.addPatternFromReader(new InputStreamReader(getClass().getResourceAsStream(
      "/patterns/common")));
    return grok;
  }
}

代码示例来源:origin: apache/metron

private void addGrok(String key, String pattern) throws GrokException {
 Grok grok = new Grok();
 InputStream patternStream = this.getClass().getResourceAsStream("/patterns/asa");
 grok.addPatternFromReader(new InputStreamReader(patternStream));
 grok.compile("%{" + pattern + "}");
 grokers.put(key, grok);
}

代码示例来源:origin: apache/metron

@Override
public void init() {
 syslogGrok = new Grok();
 InputStream syslogStream = this.getClass().getResourceAsStream("/patterns/asa");
 try {
  syslogGrok.addPatternFromReader(new InputStreamReader(syslogStream));
  syslogGrok.compile(syslogPattern);
 } catch (GrokException e) {
  LOG.error("[Metron] Failed to load grok patterns from jar", e);
  throw new RuntimeException(e.getMessage(), e);
 }
 for (Entry<String, String> pattern : patternMap.entrySet()) {
  try {
   addGrok(pattern.getKey(), pattern.getValue());
  } catch (GrokException e) {
   LOG.error("[Metron] Failed to load grok pattern {} for ASA tag {}", pattern.getValue(), pattern.getKey());
  }
 }
 LOG.info("[Metron] CISCO ASA Parser Initialized");
}

代码示例来源:origin: apache/metron

@Override
public void init() {
 grok = new Grok();
 try {
  InputStream commonInputStream = openInputStream(patternsCommonDir);
  LOG.debug("Grok parser loading common patterns from: {}", patternsCommonDir);
  if (commonInputStream == null) {
   throw new RuntimeException(
       "Unable to initialize grok parser: Unable to load " + patternsCommonDir + " from either classpath or HDFS");
  }
  grok.addPatternFromReader(new InputStreamReader(commonInputStream));
  LOG.debug("Loading parser-specific patterns from: {}", grokPath);
  InputStream patterInputStream = openInputStream(grokPath);
  if (patterInputStream == null) {
   throw new RuntimeException("Grok parser unable to initialize grok parser: Unable to load " + grokPath
       + " from either classpath or HDFS");
  }
  grok.addPatternFromReader(new InputStreamReader(patterInputStream));
  if (LOG.isDebugEnabled()) {
   LOG.debug("Grok parser set the following grok expression: {}", grok.getNamedRegexCollectionById(patternLabel));
  }
  String grokPattern = "%{" + patternLabel + "}";
  grok.compile(grokPattern);
  LOG.debug("Compiled grok pattern {}", grokPattern);
 } catch (Throwable e) {
  LOG.error(e.getMessage(), e);
  throw new RuntimeException("Grok parser Error: " + e.getMessage(), e);
 }
}

代码示例来源:origin: apache/metron

@Override
public GrokValidation validateGrokStatement(GrokValidation grokValidation) throws RestException {
  Map<String, Object> results;
  try {
    if (grokValidation.getPatternLabel() == null) {
     throw new RestException("Pattern label is required");
    }
    if (Strings.isEmpty(grokValidation.getStatement())) {
     throw new RestException("Grok statement is required");
    }
    Grok grok = new Grok();
    grok.addPatternFromReader(new InputStreamReader(getClass().getResourceAsStream(
      "/patterns/common")));
    grok.addPatternFromReader(new StringReader(grokValidation.getStatement()));
    String grokPattern = "%{" + grokValidation.getPatternLabel() + "}";
    grok.compile(grokPattern);
    Match gm = grok.match(grokValidation.getSampleData());
    gm.captures();
    results = gm.toMap();
    results.remove(grokValidation.getPatternLabel());
  } catch (Exception e) {
    throw new RestException(e);
  }
  grokValidation.setResults(results);
  return grokValidation;
}

代码示例来源:origin: co.cask.cdap/cdap-formats

protected void addPattern(Grok grok, String resource) {
 URL url = this.getClass().getClassLoader().getResource(resource);
 if (url == null) {
  LOG.error("Resource '{}' for grok pattern was not found", resource);
  return;
 }
 try {
  String patternFile = Resources.toString(url, Charsets.UTF_8);
  try {
   grok.addPatternFromReader(new StringReader(patternFile));
  } catch (GrokException e) {
   LOG.error("Invalid grok pattern from resource '{}'", resource, e);
  }
 } catch (IOException e) {
  LOG.error("Failed to load resource '{}' for grok pattern", resource, e);
 }
}

代码示例来源:origin: caskdata/cdap

protected void addPattern(Grok grok, String resource) {
 URL url = this.getClass().getClassLoader().getResource(resource);
 if (url == null) {
  LOG.error("Resource '{}' for grok pattern was not found", resource);
  return;
 }
 try {
  String patternFile = Resources.toString(url, Charsets.UTF_8);
  try {
   grok.addPatternFromReader(new StringReader(patternFile));
  } catch (GrokException e) {
   LOG.error("Invalid grok pattern from resource '{}'", resource, e);
  }
 } catch (IOException e) {
  LOG.error("Failed to load resource '{}' for grok pattern", resource, e);
 }
}

相关文章