2015-04-22 68 views
0

我試圖從Keen.io中用logstash解析JSON文件到elasticsearch中。位置和時間戳都存儲在參數如下:在logstash過濾器中解釋來自Keen.io JSON文件的位置

{ 
    "result": 
    [ 
    { 
     "keen": 
     { 
     "timestamp": "2014-12-02T12:23:51.000Z", 
     "created_at": "2014-12-01T23:25:31.396Z", 
     "id": "XXXX", 
     "location": 
     { 
      "coordinates": [-95.8, 36.1] 
     } 
     } 
    } 
    ] 
} 

我的過濾器目前看起來是這樣的:

input { 
    file { 
    path => ["test.json"] 
    start_position => beginning 
    type => json 
    } 
} 

filter { 
    json { 
    source => message 
    remove_field => message 
    } 
} 

output { 
    stdout { codec => rubydebug } 
} 

如何可以解析了「時間戳」和「位置」字段,以便它們用於Elasticsearch中的@timestamp和@ geoip.coordinates?

更新: 我試過這個變化,沒有運氣。文檔是非常基本的 - 我誤解如何引用JSON字段?有沒有添加調試輸出幫助的方法?我試過How to debug the logstash file pluginPrint a string to stdout using Logstash 1.4?,但都不起作用。

filter { 
    json { 
    source => message 
    remove_field => message 
    } 
    if ("[result][0][keen][created_at]") { 
    date { 
     add_field => [ "[timestamp]", "[result][0][keen][created_at]" ] 
     remove_field => "[result][0][keen][created_at]" 
    } 
    } 

更新2:

日期是現在的工作,仍然需要獲得位置的工作。

filter { 
    json { 
    source => message 
    remove_field => message 
    add_tag => ["valid_json"] 
    } 
    if ("valid_json") { 
    if ("[result][0][keen][created_at]") { 
     date { 
     match => [ "[result][0][keen][created_at]", "ISO8601" ] 
     } 
    } 
    } 
} 
+1

我不認爲'add_field'是正確的,你需要'update_field'因爲時間戳字段已經存在。無論如何,它是否跳入了「if(...)」部分?在那裏添加標籤以查明。 –

+0

但'update_field'不存在?標籤上的好主意,想知道他們是爲了什麼。 – parsley72

+1

你想要的是'date'過濾器的'match'屬性,比如'filter { date} {match => [「[result] [0] [keen] [created_at]」,「MMM dd YYYY HH: mm:ss「] } }' –

回答

0

Keen.io的「created_at」字段中,儲存在ISO 8601 format,因此可以很容易地通過日期過濾器進行解析。 Lat/long座標可以通過將Keen.io的現有座標複製到logstash的geoip.coordinates數組中來設置。

input { 
    file { 
    path => ["data.json"] 
    start_position => beginning 
    type => json 
    } 
} 

filter { 
    json { 
    source => message 
    remove_field => message 
    add_tag => ["valid_json"] 
    } 
    if ("valid_json") { 
    if ("[result][0][keen][created_at]") { 
     date { 
     # Set @timestamp to Keen.io's "created_at" field 
     match => [ "[result][0][keen][created_at]", "ISO8601" ] 
     } 
    } 
    if ("[result][0][keen][location][coordinates]") { 
     mutate { 
     # Copy existing co-orndiates into geoip.coordinates array 
     add_field => [ "[geoip][coordinates]", "%{[result][0][keen][location][coordinates][0]}" ] 
     add_field => [ "[geoip][coordinates]", "%{[result][0][keen][location][coordinates][1]}" ] 
     remove_field => "[result][0][keen][location][coordinates]" 
     } 
    } 
    } 
} 

output { 
    stdout { codec => rubydebug } 
}