使用logstash时,文档中的@timestamp为epoch-millis

时间:2019-01-25 11:53:47

标签: ruby elasticsearch logstash

在我们的项目中完成的PoC中,我们正在尝试使用Logstash而不是我们自己的基于Java的索引模块来将数据推送到ElasticSearch。传入的json数据没有@timestamp字段。因此,使用Logstash时,它将以ISO格式添加该字段。但是我们已经有了该ES索引的特定映射,并且它要求我们以时代毫秒格式推送@timestamp

我尝试使用红宝石过滤器将@timestamp转换为纪元,但到目前为止还没有运气。我们有什么方法可以通过Logstash将记录提取到ES,而@timestamp则为纪元-毫秒格式?

我正在使用Logstash 6.5.4和ES 6.2.2

更新:尝试了答案中的建议后,我的conf文件如下所示:

input { stdin { } }

filter {
    ruby {
        code => "
            epoch_ts = event.timestamp.time.localtime.strftime('%s').to_i
            event.set( 'epoch', epoch_ts  )
        "
    }
}

output {
    elasticsearch {
        hosts => ["localhost:9200"]
        index => "myindex"
        script_type => "inline"
        script => 'ctx._source.@timestamp = params.event.get("epoch")'
    }
    stdout { codec => rubydebug }
}

但是仍然不起作用。 @timestamp值完全不变。现在,我还需要删除该额外字段epoch

2 个答案:

答案 0 :(得分:0)

此红宝石代码应为您工作:

ruby {
    code => "
        epoch_ts = event.timestamp.time.localtime.strftime('%s').to_f
        event.set( '@timestamp', epoch_ts  )
    "
}

答案 1 :(得分:0)

经过一段时间的网上搜索,我终于放弃了这种方法。相反,我强迫ES使用this docvalue_fields方法在epoch_millis中返回@timestamp。

相关问题