如何使用 Logstash 拆分字段

How to use Logstash split with field

我在文件 data.json 中存储了以下数据:

{"result":[{"number":"1"},{"number":"2"}]}

这是我的 logstash 配置文件:

input {
   file {
       path => "C:/work/apps/ELK/data.json"
       start_position => "beginning"
       sincedb_path => "/dev/null"
   }
}

filter {
   split {     
       field => "result"
   }
}

output {
   elasticsearch {
       hosts => "localhost:9200"
       index => "so"
       document_type => 1
   }
   stdout { codec => rubydebug }
}

当我 运行 Logstash 使用此配置和上面的数据时,我在 Elastic 中只得到一个条目和整个文本(见下图)。

当我修改数据以添加新行时:

"result":[
{"number":"1"},
{"number":"2"}]

result 仍然被忽略,但这次我根据新行进行了拆分。我在数据库中收到三条消息:

我尝试使用原始 json 文件提出的 解决方案,但同样没有解析任何内容(如第一种情况)。我不明白为什么。

拆分过滤器不起作用,因为字段结果不存在。要创建它,您需要解析您正在从文件中读取的 json,这将创建字段。

要解析 json,请使用 json codec on your input or the json filter

仅供参考,最终使用的配置:

input {
   file {
      path => "C:/work/apps/ELK/data.json"
      start_position => "beginning"
      sincedb_path => "/dev/null"
   }
}

filter {
    json {
        source => "message"
        remove_field => ["message", "host", "path"]
    }
    split {
        field => "result"
        add_field => {
            "number" => "%{[result][number]}"           
        }
        remove_field => "result"
   }
}

output {
    elasticsearch {
        hosts => "localhost:9200"
        index => "so"
        document_type => 1
    }
    stdout { codec => rubydebug }
}