索引基于logstash中转换的unix时间戳的值

时间:2017-02-15 14:14:01

标签: elasticsearch logstash logstash-grok logstash-configuration

我需要根据UNIX时间戳(自纪元以来的秒数)字段的值来索引记录,但索引需要采用YYYY-MM-dd的形式。但是,timestamp字段的值必须保持为UNIX时间戳。

所以问题是两部分。

  1. 如何在不破坏UNIX时间戳的情况下将时间戳文件转换为YYYY-MM-dd
  2. 如何将YYYY-MM-dd值应用于索引?
  3. 这是我到目前为止所拥有的。

    input {
        tcp {
                port => 5000
        }
    }
    
    filter {
        csv {
                separator => "  " # <- this white space is actually a tab
                skip_empty_columns => true
                # other fields omitted for brevity
                columns => ["timestamp"]
        }
    
        #grok {
        #       match => {"indexdate" => "%{NUMBER:timestamp}"}
        #}
    
        date {
                match => ["timestamp", "YYYY-MM-DD"]
                target => "@timestamp"
        }
    
        mutate {
                # omitted for brevity
                remove_field => []
        }
    }
    
    output {
        elasticsearch {
                hosts => "elasticsearch:9200"
                index => "indexname-%{+YYYY-MM-dd}"
        }
    }
    

    到目前为止,我做了几次不同的尝试但没有运气。

1 个答案:

答案 0 :(得分:1)

只需像这样更改您的date过滤器即可。您在date过滤器中使用的模式应该与您正在解析的字段的模式匹配,因为您有纪元秒数,那么模式应该是UNIX

date {
        match => ["timestamp", "UNIX"]
        target => "@timestamp"
}

请注意,您可能需要在convert过滤器中添加csv设置,以确保timestamp字段为整数

csv {
   ...
   convert => { "timestamp" => "integer" }
}