首页 > 解决方案 > 使用 Grok 进行模式匹配时如何考虑空值?

问题描述

我正在尝试读取Apache NiFi使用中的日志数据,grok但无法获取所需的输出。这是我的示例数据:

[2019-07-16 22:20:16] local.INFO: news.index {"mobile":"959404576540","message":Mozilla/5.0 (Linux; Android 8.0.0; ATU-L42 Build/HUAWEIATU-L42; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36 a/2.7.0}

我尝试使用以下表达式,但对我没有用。

%{SYSLOG5424SD}%{JAVACLASS}: %{JAVACLASS} {%{QS}:%{QS},%{QS}:%{QS}} 

如何从上述数据中提取2019-07-16 22:20:16, 959404576540, ?Mozilla/5.0 (Linux; Android 8.0.0; ATU-L42 Build/HUAWEIATU-L42; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36 a/2.7.0

编辑:

@Emma您的答案有效,但如果我的数据包含任何空值,则它不起作用。例如:

[2019-07-16 22:20:16] local.INFO: news.index {"mobile":"8765453673","message":null}

标签: regexapache-nifilogstash-grok

解决方案


This expression,

\[([^]]+?)\](.*?)"mobile":"\s*(\d+)\s*"\s*,\s*"message"\s*:\s*([^}]*?)\s*\}

for instance might be a start to extract those data.


The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.


推荐阅读