Logstash - 如何使用不带目标的拆分过滤器拆分数组?

时间:2015-05-31 14:26:17

标签: elasticsearch logstash

我试图将JSON数组拆分为多个事件。这是一个示例输入:

{"results" : [{"id": "a1", "name": "hello"}, {"id": "a2", "name": "logstash"}]}

这是我的过滤器和输出配置:

filter {
  split {
    field => "results"
  }
}
stdout { 
  codec => "rubydebug"
}

这会产生2个事件,每个事件对应于数组中的每个JSON。它接近我正在寻找的东西:

{                                              
       "results" => {                          
          "id" => "a1",                        
        "name" => "hello"                      
    },                                         
      "@version" => "1",                       
    "@timestamp" => "2015-05-30T18:33:21.527Z",
          "host" => "laptop",                                      
}                                              
{                                              
       "results" => {                          
          "id" => "a2",                        
        "name" => "logstash"                   
    },                                         
      "@version" => "1",                       
    "@timestamp" => "2015-05-30T18:33:21.527Z",
          "host" => "laptop",                                   
}

问题是嵌套"结果"部分。 "结果"是target参数的默认值。 有没有办法在不生成嵌套JSON的情况下使用拆分过滤器,并得到如下内容:

{                                                                     
          "id" => "a1",                        
        "name" => "hello"                      
      "@version" => "1",                       
    "@timestamp" => "2015-05-30T18:33:21.527Z",
          "host" => "laptop",                                      
}                                              
{                                              
          "id" => "a2",                        
        "name" => "logstash"                   
      "@version" => "1",                       
    "@timestamp" => "2015-05-30T18:33:21.527Z",
          "host" => "laptop",                                   
}

目的是将此提供给ElasticSearch输出,每个事件都是一个包含document_id =>的文档。 " ID&#34 ;.欢迎任何好的解决方案!

1 个答案:

答案 0 :(得分:3)

如果你知道所有字段是什么(就像你看到的那样),你可以简单地重命名字段:

    mutate {
            rename => [
                    "[results][id]", "id",
                    "[results][name]", "name"
            ]
            remove_field => "results"
    }

如果您不知道所有字段是什么,您可以编写一个ruby代码过滤器,执行event['results'].each...并从结果的子字段中创建新字段。