如何使用grok过滤{“foo”:“bar”,“bar”:“foo”}只获取foo字段?

时间:2017-04-07 23:06:08

标签: elasticsearch elastic-stack logstash-grok grok

我复制了

{"name":"myapp","hostname":"banana.local","pid":40161,"level":30,"msg":"hi","time":"2013-01-04T18:46:23.851Z","v":0}

来自https://github.com/trentm/node-bunyan并将其保存为我的logs.json。我试图通过LogStash只导入两个字段(名称和消息)到ElasticSearch。问题是我依赖于一种我无法实现的过滤器。好吧,我已成功导入这样的行作为单个消息,但在我的实际情况下肯定不值得。

那就是说,如何只将名称和消息导入ElasticSearch?我使用http://grokdebug.herokuapp.com/测试了几个替代方案,以达到一个有用的过滤器而根本没有成功。

例如,%{GREEDYDATA:message}会将整行作为唯一消息,但如何拆分它并忽略除name和msg字段以外的所有消息?

最后,我计划在这里使用:

input {
    file {
        type => "my_type"
        path => [ "/home/logs/logs.log" ]
        codec => "json"
    }   
}

filter {     

   grok {
            match => { "message" => "data=%{GREEDYDATA:request}"}        
        }   
#### some extra lines here probably
} 

output
{ 
  elasticsearch {
    codec => json
    hosts => "http://127.0.0.1:9200"
    index => "indextest"
  }

    stdout { codec => rubydebug }
} 

1 个答案:

答案 0 :(得分:0)

我刚刚浏览了available Logstash filters列表。 prune filter符合您的需求。

假设您已安装prune filter,您的配置文件应如下所示:

input {
  file {
    type => "my_type"
    path => [ "/home/logs/logs.log" ]
    codec => "json"
  }
}

filter {
  prune {
    whitelist_names => [
      "@timestamp",
      "type",
      "name",
      "msg"
    ]
  }
} 

output { 
  elasticsearch {
    codec => json
    hosts => "http://127.0.0.1:9200"
    index => "indextest"
  }

  stdout { codec => rubydebug }
} 

请注意,您需要让type让Elasticsearch将其编入正确的类型。如果您要查看Kibana上的数据,则需要@timestamp

相关问题