首页 > 解决方案 > 使用 Logstash 输出 csv 插件从 ElasticSearch 输出文档元数据

问题描述

我正在尝试使用 Logstash 将 ES 中的 _id 元数据字段输出到 CSV 文件中。

{
  "_index": "data",
  "_type": "default",
  "_id": "vANfNGYB9XD0VZRJUFfy",
  "_version": 1,
  "_score": null,
  "_source": {
    "vulnid": "CVE-2018-1000060",
    "product": [],
    "year": "2018",
    "month": "02",
    "day": "09",
    "hour": "23",
    "minute": "29",
    "published": "2018-02-09T18:29:02.213-05:00",
  },
  "sort": [
    1538424651203
  ]
}

我的logstash输出过滤器是:

output { csv {  fields => [ "_id", "vulnid", "published"]  path =>
"/tmp/export.%{+YYYY-MM-dd-hh-mm}.csv" } }

我得到输出:

,CVE-2018-1000060,2018-02-09T18:29:02.213-05:00

但我想得到:

vANfNGYB9XD0VZRJUFfy,CVE-2018-1000060,2018-02-09T18:29:02.213-05:00

如何将元数据 _id 输出到 csv 文件中?我是否指定像“_id”或“@_id”或“@id”这样的字段并不重要。

标签: elasticsearchlogstash

解决方案


好吧,logstash 无法从您的输入中获取“_id”字段,因为您一定没有将选项docinfo设置为 true。

docinfo 有助于包含 elasticsearch 文档信息,例如索引、类型 _id 等。请在此处查看更多信息https://www.elastic.co/guide/en/logstash/current/plugins-inputs-elasticsearch.html#插件输入-elasticsearch-docinfo

使用您的输入插件作为

input {
  elasticsearch {
    hosts => "hostname"
    index => "yourIndex"
    query => '{ "query": { "query_string": { "query": "*" } } }' //optional
    size => 500 //optional
    scroll => "5m" //optional
    docinfo => true
  }
}

推荐阅读