首页 > 解决方案 > 覆盖 @timestamp 以在索引名称中获得正确的 %{+yyyy.MM.dd}

问题描述

我让 Filebeat 逐行读取 json 文件,在每个 json 事件中,我已经有了timestamp字段(格式:2021-03-02T04:08:35.241632)

处理后,有一个新字段@timestamp(可能添加了元字段 Filebeat,等于当前时间),并且似乎是索引模式%{+yyyy.MM.dd}https://www.elastic.co/guide/en/beats/filebeat/current/elasticsearch-output。 html#index-option-es ) 被配置到该字段。我想@timestamp用时间戳处理器覆盖:https ://www.elastic.co/guide/en/beats/filebeat/current/processor-timestamp.html但不起作用,可能是布局设置不正确?

- timestamp:
      field: timestamp
      layouts:
        - '2021-03-02T03:29:29.787331'
2021-03-07T11:29:39.382+0700    DEBUG   [processor.timestamp]   timestamp/timestamp.go:173  Failure parsing time field. {"error": "failed parsing time field timestamp='2021-03-02T03:29:29.787331'", "errorCauses": [{"error": "failed using layout [2021-03-02T03:29:29.787331] cannot parse [-03-02T03:29:29.787331] as [1]"}]}

我还尝试了另一种使用Date.parse但不起作用的解析时间戳的方法,不确定在 Filebeat 中实现的 ECMA 5.1 是否缺少某些内容:

var date = new Date.parse('2021-03-02T03:29:29.787331');
    event.Put('@metadata.index_suffix', date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate());
  "error": {
    "message": "TypeError: Not a constructor at process (/Users/thoong/Projects/shodan/shodan-trends/transform.js:25:16(106))"
  },

所以我的timestamp格式是2021-03-02T03:29:29.787331,我想问一下layouts处理器或解析的正确方法是什么Date.parse

解决方案,这是来自@Val 答案的正确布局

- timestamp:
      field: timestamp
      layouts:
        - '2006-01-02T15:04:05.000000'

标签: elasticsearchfilebeat

解决方案


如果您的字段已具有 ISO8601 格式,则无需指定layouts参数。timestamp

但是,如果您的timestamp字段有不同的布局,您必须在布局部分指定一个非常具体的参考日期,Mon Jan 2 15:04:05 MST 2006您也可以提供一个test日期。(更多信息

因此,在您的情况下,它将是:

- timestamp:
    field: timestamp
    ignore_missing: true
    ignore_failure: true
    layouts:
      - '2006-01-02T15:04:05.000000'
    test:
      - '2021-03-02T03:29:29.787331'

推荐阅读