2015-04-22 66 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 } 
}