【问题标题】:Logstash "add_field" saves "%{...}" as value when key value pair missing in JSON当 JSON 中缺少键值对时,Logstash "add_field" 将 "%{...}" 保存为值
【发布时间】: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


    【解决方案1】:

    您可以使用修剪过滤器解决此问题,其中 blacklist_names 的默认值是删除未解析的字段引用。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 2022-06-30
      • 2019-12-10
      相关资源
      最近更新 更多