解释日志存储筛选器中 Keen.io JSON 文件中的位置



我正在尝试将带有logstash的 Keen.io 的JSON文件解析为elasticsearch。位置和时间戳存储在如下参数中:

{
  "result":
  [
    {
      "keen":
      {
        "timestamp": "2014-12-02T12:23:51.000Z",
        "created_at": "2014-12-01T23:25:31.396Z",
        "id": "XXXX",
        "location":
        {
          "coordinates": [-95.8, 36.1]
        }
      }
    }
  ]
}

我的过滤器目前如下所示:

input {
  file {
    path => ["test.json"]
    start_position => beginning
    type => json
  }
}
filter {
  json {
    source => message
    remove_field => message
  }
}
output {
  stdout { codec => rubydebug }
}

如何解析"时间戳"和"位置"字段,以便将它们用于 Elasticsearch 中的@timestamp和 @geoip.坐标?

更新:我尝试过这种变体,但没有运气。文档非常基本 - 我是否误解了如何引用 JSON 字段?有没有办法添加调试输出来提供帮助?我尝试了如何使用 Logstash 1.4 调试 logstash 文件插件并打印字符串到标准输出?但两者都不起作用。

filter {
  json {
    source => message
    remove_field => message
  }
  if ("[result][0][keen][created_at]") {
    date {
      add_field => [ "[timestamp]", "[result][0][keen][created_at]" ]
      remove_field => "[result][0][keen][created_at]"
    }
  }

更新 2:

日期现在正在工作,仍然需要获得位置工作。

filter {
  json {
    source => message
    remove_field => message
    add_tag => ["valid_json"]
  }
  if ("valid_json") {
    if ("[result][0][keen][created_at]") {
      date {
        match => [ "[result][0][keen][created_at]", "ISO8601" ]
      }
    }
  }
}

Keen.io的"created_at"字段以ISO 8601格式存储,因此可以通过日期过滤器轻松解析。纬度/经度坐标可以通过将 Keen.io 的现有坐标复制到 logstash 的 geoip.坐标数组中来设置。

input {
  file {
    path => ["data.json"]
    start_position => beginning
    type => json
  }
}
filter {
  json {
    source => message
    remove_field => message
    add_tag => ["valid_json"]
  }
  if ("valid_json") {
    if ("[result][0][keen][created_at]") {
      date {
        # Set @timestamp to Keen.io's "created_at" field
        match => [ "[result][0][keen][created_at]", "ISO8601" ]
      }
    }
    if ("[result][0][keen][location][coordinates]") {
      mutate {
        # Copy existing co-orndiates into geoip.coordinates array
        add_field => [ "[geoip][coordinates]", "%{[result][0][keen][location][coordinates][0]}" ]
        add_field => [ "[geoip][coordinates]", "%{[result][0][keen][location][coordinates][1]}" ]
        remove_field => "[result][0][keen][location][coordinates]"
      }
    }
  }
}
output {
  stdout { codec => rubydebug }
}

最新更新