多行日志的顺序不正确

时间:2021-02-18 17:50:52

标签: elasticsearch logstash elk logstash-file

我有一个看起来像这样的日志:

1613635264 host1 AAA 0.11 0.09 0.12 16 13
1613635264 host1 BBB 0.21 0.23 176141
1613635264 host2 AAA 2.08 1.76 1.38 4 3
1613635264 host2 BBB 6.21 0.12 228981
1613635264 host3 AAA 0.58 1.12 1.75 16 0
1613635264 host3 BBB 4.46 0.11 254346
1613635265 host4 AAA 1.07 1.11 1.38 16 4
1613635265 host5 AAA 18.21 17.97 19.19 5 2
1613635265 host4 BBB 3.18 0.40 105858
1613635265 host5 BBB 64.69 1.08 418177

AAA 和 BBB 行不按顺序排列,但时间戳(第一列)对于该对是相同的。

logstash 可以合并这两行吗?

像这样:

{ 
 time: 1613635264, 
 host: host1, 
 metric1: 0.11 
 metric2: 0.09 
 metric3: 0.12 
 metric4: 16 
 metric5: 13
 metric6: 0.21 
 metric7: 0.23 
 metric8: 176141
}

我想在 elasticsearch 中插入相同的文档。 这可能吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我设置了一个唯一的 document_id 来匹配 2 对行,设置 doc_as_upsert=true 和 action=update。

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    doc_as_upsert => "true"
    action => "update"
    document_id => "%{@timestamp}%{hostname}"
  }
}