【问题标题】:Dealing with "null" fields and add_field directive in logstash在 logstash 中处理“null”字段和 add_field 指令
【发布时间】:2015-09-24 00:43:04
【问题描述】:

我有一个包含 100 条记录的 json 文件。它的结构是这样的:

{
    "_app1": {
        "test": "test"
    },
    "location": {
        "longitude": 40.400000000000006,
        "latitude": 40.400000000000006,
        "country": "CH"
    },
    "timestamp": "2015-08-23"
}

我需要从 Kibana 创建一些地理可视化,所以我在映射文件中定义了 geo_point 类型:

"location" : { "type" : "geo_point" }

由于位置字段必须遵循一定的结构 (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html),我在 logstash 配置文件中做了以下操作:

input {
  stdin {
    type => "json"
  }
}

filter {
   json {
        source => "message"
    }
   mutate {
        rename => [ "location", "newlocation" ]
        add_field => { "location" => "%{[newlocation][latitude]},%{[newlocation][longitude]}" }
    }

}

output {
    elasticsearch {
...
    }
}

通过这个配置,我发现大多数文档都在 elasticsearch 中被索引,但是(这就是问题所在)如果经度或纬度为“null”,则寄存器不会被索引。所以,例如,这条记录:

{
    "_app1": {
        "test": "test"
    },
    "location": {
        "longitude": null,
        "latitude": 40.400000000000006,
        "country": "CH"
    },
    "timestamp": "2015-08-23"
}

不会在 ES 中被索引。我的问题是,如何索引 ES 中的所有寄存器,并为那些经纬度与 null 不同的人创建一个新字段。

我尝试过这样的事情:

...
filter {
   json {
        source => "message"
    }
   mutate {
        rename => [ "location", "newlocation" ]
    }
   if [newlocation][latitude] and [newlocation][longitude] {
     mutate {
        add_field => { "location" => "%{[newlocation][latitude]},%{[newlocation][longitude]}" }
     }
   }
}
..

但不起作用,有什么想法吗?

【问题讨论】:

  • 您的示例 JSON 格式似乎不正确,即不可能有这个 {"_app1": "location": {...。您是否缺少一对花括号?如果您通过 jsonlint.com 运行 JSON,则会收到错误消息。
  • 你说得对,谢谢。我已经编辑了这个问题。

标签: json elasticsearch logstash kibana


【解决方案1】:

您的配置在此测试工具中适用于我:

input {
    stdin {  }
}

filter {
  json {
        source => "message"
  }

  mutate {
        rename => [ "location", "newlocation" ]
  }

  if [newlocation][latitude] and [newlocation][longitude] {
     mutate {
        add_field => { "location" => "%{[newlocation][latitude]},%{[newlocation][longitude]}" }
     }
   }
}

output {
    stdout {
        codec => rubydebug
    }
}

【讨论】:

  • 谢谢,你说得对,我的问题来自另一边
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
相关资源
最近更新 更多