Logstash/Elasticsearch CSV 字段类型、日期格式和多字段 (.raw)

Logstash/Elasticsearch CSV Field Types, Date Formats and Multifields (.raw)

我一直在尝试使用 Logstash 中的 CSV 过滤器将制表符分隔的文件导入 Elasticsearch。获取数据实际上非常容易,但是当我在 Kibana 中查看数据时,我无法正确获取字段类型。日期和整数继续以字符串形式出现,因此我无法按日期绘制或对整数(总和、平均值等)进行任何分析函数。

我在获取要填充的字段的 .raw 版本时也遇到了问题。例如,在设备中我有类似 "HTC One" 的数据,但如果我在 Kibana 中绘制饼图,它会显示为两个单独的分组 "HTC" 和 "One"。当我尝试绘制 device.raw 图表时,它作为一个缺失字段出现。根据我的阅读,Logstash 似乎应该自动创建每个字符串字段的原始版本,但这似乎并没有发生。

我一直在筛选文档、google 和堆栈,但没有找到解决方案。任何想法表示赞赏!谢谢

配置文件:

#logstash.conf
input {  
      file {
          path => "file.txt"
          type => "event"
          start_position => "beginning"
          sincedb_path => "/dev/null"
      }
}

filter {  
    csv {
      columns => ["userid","date","distance","device"]
      separator => "    "
    }
}

output {  
    elasticsearch {
        action => "index"
        host => "localhost"
        port => "9200"
        protocol => "http"
        index => "userid" 
        workers => 2
        template => template.json

    }
    #stdout {
    #    codec => rubydebug
    #}
}

这是模板文件:

#template.json:
{
    "template": "event",
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0,
        "index" : {
            "query" : { "default_field" : "userid" } 
        }
    },
    "mappings": {
        "_default_": { 
            "_all": { "enabled": false },
            "_source": { "compress": true },
            "dynamic_templates": [
                {
                    "string_template" : { 
                        "match" : "*",
                        "mapping": { "type": "string", "index": "not_analyzed" },
                        "match_mapping_type" : "string"
                     } 
                 }
             ],
             "properties" : {
                "date" : { "type" : "date", "format": "yyyy-MM-dd HH:mm:ss"},
                "device" : { "type" : "string", "fields": {"raw": {"type":  "string","index": "not_analyzed"}}},
                "distance" : { "type" : "integer"}
        }
    }
}

明白了 - 模板名称就是索引。所以 "template" : "event" 行应该是 "template" : "userid"

我找到了另一种(更简单的)方法来指定字段的类型。您可以使用 logstash 的 mutate 过滤器来更改字段的类型。只需在您的 csv 过滤器之后添加以下过滤器到您的 logstash 配置

  mutate {
    convert => [ "fieldname", "integer" ]
  }

有关详细信息,请查看 logstash docs - mutate convert