我尝试使用logstash将一个JSON文件从Keen.io解析为elasticsearch。位置和时间戳存储在以下参数中:
{
"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 plugin和Print 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" ]
}
}
}
}
答案 0 :(得分:0)
Keen.io" created_at"字段存储在ISO 8601 format中,因此可以通过日期过滤器轻松解析。可以通过将Keen.io的现有坐标复制到logstash的geoip.coordinates数组来设置Lat / long坐标。
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 }
}