简体   繁体   English

从 Logstash 中的日志文件解析一组 JSON 对象

[英]Parsing an array of JSON objects from logfile in Logstash

I have logs in the following type of format:我有以下格式的日志:

2021-10-12 14:41:23,903716 [{"Name":"A","Dimen":[{"Name":"in","Value":"348"},{"Name":"ses","Value":"asfju"}]},{"Name":"read","A":[{"Name":"ins","Value":"348"},{"Name":"ses","Value":"asf5u"}]}]
2021-10-12 14:41:23,903716 [{"Name":"B","Dimen":[{"Name":"in","Value":"348"},{"Name":"ses","Value":"a7hju"}]},{"Name":"read","B":[{"Name":"ins","Value":"348"},{"Name":"ses","Value":"ashju"}]}]

Each log on a new line.每次登录新行。 Problem is I want each object from the single line in the top level array to be a separate document and parsed accordingly.问题是我希望顶级数组中单行中的每个对象都是一个单独的文档并进行相应的解析。

I need to parse this and send it to Elasticsearch.我需要解析它并将其发送到 Elasticsearch。 I have tried a number of filters, grok, JSON, split etc and I cannot get it to work the way I need to and I have little experience with these filters so if anyone can help it would be much appreciated.我已经尝试了许多过滤器、grok、JSON、split 等,但我无法让它按照我需要的方式工作,而且我对这些过滤器的经验很少,所以如果有人能提供帮助,我将不胜感激。

The JSON codec is what I would need if I can remove the Text/timestamp from the file.如果我可以从文件中删除文本/时间戳,我将需要JSON 编解码器

"If the data being sent is a JSON array at its root multiple events will be created (one per element)" “如果发送的数据是其根处的 JSON 数组,则将创建多个事件(每个元素一个)”

If there is a way to do that, this would also be helpful如果有办法做到这一点,这也会有所帮助

This is the config example for your usecase:这是您的用例的配置示例:

input { stdin {} }
filter {
grok {
        match => { "message" => "%{DATA:date},%{DATA:some_field} %{GREEDYDATA:json_message}" }
      }

#Use the json plugin to translate raw to json
json { source => "json_message" target => "json" }

#and split the result to dedicated raws
split { field => "json" }

}
output {
  stdout {
    codec => rubydebug 
  }
}

If you need to parse the start of the log as date, you can use the grok with the date format or connect two fields and set than as source to the date plugin.如果您需要将日志的开始解析为日期,您可以使用带有日期格式的 grok 或连接两个字段并将比设置为日期插件的源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM