2015-03-02 92 views
2

我一直在使用Logstash中的CSV篩選器來獲取製表符分隔文件到Elasticsearch中。獲取數據實際上非常簡單,但是當我查看Kibana中的數據時,我無法使字段類型正確顯示。日期和整數繼續以字符串形式出現,所以我無法按日期進行繪圖或對整數(總和,平均值等)執行任何分析函數。Logstash/Elasticsearch CSV字段類型,日期格式和多字段(.raw)

我也無法獲得.raw版本的字段填充。例如,在設備中,我有「HTC One」等數據,但是如果我在Kibana中做了餅圖,它將顯示爲兩個單獨的分組「HTC」和「One」。當我嘗試繪製device.raw時,它會出現一個缺失的字段。從我讀過的內容看來,Logstash似乎應該自動創建每個字符串字段的原始版本,但似乎並沒有發生。

我一直在瀏覽文檔,谷歌和堆棧,但還沒有找到解決方案。任何想法讚賞!謝謝。

配置文件:

#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"} 
     } 
    } 
} 

回答

0

想通了 - 模板名稱是索引。所以「模板」:「事件」行應該是「模板」:「用戶標識」

+0

的「模板」,在模板的根屬性實際上是一個模式,因此「用戶*」將匹配以「用戶」一詞開頭的任何索引。當您將其張貼到elasticsearch時,會給出該模板的名稱。文檔:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html – 2015-03-02 16:49:31

+0

@econgineer我也(未成功地)試圖將csv過濾器的列轉換爲某些類型,例如而不是字符串到一個整數。你有沒有碰運氣? – markus 2015-03-02 17:02:59

+0

@markus,就像我說的,上面的工作一旦我確定「template」屬性被標記爲與我在logstash.conf文件中定義的索引名稱相同。 – econgineer 2015-03-03 08:23:04

0

我發現了另一種(更簡單)的方式來指定字段的類型。您可以使用logstash的mutate過濾器來更改字段的類型。只需將您的CSV過濾器後添加以下過濾器,以你的logstash配置

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

詳細檢查了logstash docs - mutate convert

+0

還有一個csv過濾器的「轉換」屬性,但它不適用於我。任何想法,爲什麼,@markus? – 2016-08-03 11:04:32

+0

@DmitriLikhachev對不起,但我還沒有使用過。 – markus 2016-08-04 07:59:23