【发布时间】:2019-08-01 09:11:04
【问题描述】:
add_field => {"ExampleFieldName" => "%{[example][jsonNested1][jsonNested2]}"}
我的 Logstash 接收到来自 Filebeat 的 JSON,其中包含对象 example,该对象本身包含对象 jsonNested1,其中包含键值对(键为 jsonNested2)。
如果jsonNested1 存在且jsonNested2 存在且包含一个值,则该值将正确保存在Elasticsearch 中的ExampleFieldName 中。
{
"example": {
"jsonNested1": {
"jsonNested2": "exampleValue"
}
}
}
在这种情况下,ExampleFieldName 将包含 exampleValue。
{
"example": {
"jsonNested1": {
}
}
}
在这种情况下,我希望 ExampleFieldName 包含一个空字符串或根本不包含任何值(或者一开始就不创建)。
但碰巧ExampleFiledName 包含字符串%{[example][jsonNested1][jsonNested2]}。
我已经通过在执行add_field之前首先检查嵌套键值对是否存在找到了解决方案。
if [example][jsonNested1][jsonNested2] {
mutate {
add_field => {"ExampleFieldName" => "%{[example][jsonNested1][jsonNested2]}"}
}
}
此解决方案有效,但我不敢相信这是最好的方法。我觉得很奇怪,当键值对不存在时,Logstash 甚至将%{[example][jsonNested1][jsonNested2]} 保存为字符串。我希望它能够认识到这一点,并且在这种情况下根本不保存任何价值。
如果必须检查一个字段,if 语句是一种可接受的解决方案。但目前我正在使用大约 50 个字段的 Logstash 配置。我应该在那里创建 50 个 if 语句吗?
【问题讨论】:
标签: elasticsearch logstash logstash-configuration filebeat logstash-file