logstash输出到elasticsearch索引和映射

时间:2017-02-13 14:47:14

标签: elasticsearch logstash

我试图将logstash输出设置为elasticsearch,但我不确定如何使用我在elasticsearch中定义的映射...

在Kibana,我这样做了:

创建了一个索引和映射,如下所示:

PUT /kafkajmx2
{
  "mappings": {
    "kafka_mbeans": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "integer"
        },
        "host": {
          "type": "keyword"
        },
        "metric_path": {
          "type": "text"
        },
        "type": {
          "type": "keyword"
        },
        "path": {
          "type": "text"
        },
        "metric_value_string": {
          "type": "keyword"
        },
        "metric_value_number": {
          "type": "float"
        }
      }
    }
  }

}

可以像这样写数据:

POST /kafkajmx2/kafka_mbeans
{
  "metric_value_number":159.03478490788203,
  "path":"/home/usrxxx/logstash-5.2.0/bin/jmxconf",
  "@timestamp":"2017-02-12T23:08:40.934Z",
  "@version":"1","host":"localhost",
  "metric_path":"node1.kafka.server:type=BrokerTopicMetrics,name=TotalFetchRequestsPerSec.FifteenMinuteRate",
  "type":null


}

现在我的logstash输出如下所示:

input {
        kafka {
                kafka details here
        }

}
output {

    elasticsearch {
            hosts => "http://elasticsearch:9050"
            index => "kafkajmx2"

    }

}

它只是将它写入kafkajmx2索引,但是当我在kibana中这样查询时它没有使用地图:

get /kafkajmx2/kafka_mbeans/_search?q=*
{


}

我得到了回复:

      {
        "_index": "kafkajmx2",
        "_type": "logs",
        "_id": "AVo34xF_j-lM6k7wBavd",
        "_score": 1,
        "_source": {
          "@timestamp": "2017-02-13T14:31:53.337Z",
          "@version": "1",
          "message": """
{"metric_value_number":0,"path":"/home/usrxxx/logstash-5.2.0/bin/jmxconf","@timestamp":"2017-02-13T14:31:52.654Z","@version":"1","host":"localhost","metric_path":"node1.kafka.server:type=SessionExpireListener,name=ZooKeeperAuthFailuresPerSec.Count","type":null}

"""
        }
      }

如何告诉它在logstash输出中使用地图kafka_mbeans

- - - - - - - - 编辑

我尝试了这样的输出,但仍然得到了相同的结果:

output {

        elasticsearch {
                hosts => "http://10.204.93.209:9050"
                index => "kafkajmx2"
                template_name => "kafka_mbeans"
                codec => plain {
                        format => "%{message}"
                }

        }

}

弹性搜索中的数据应如下所示:

{
  "@timestamp": "2017-02-13T14:31:52.654Z", 
  "@version": "1", 
  "host": "localhost", 
  "metric_path": "node1.kafka.server:type=SessionExpireListener,name=ZooKeeperAuthFailuresPerSec.Count", 
  "metric_value_number": 0, 
  "path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf", 
  "type": null
}

--------编辑2 --------------

我至少通过添加如下过滤器来解析json:

input {
        kafka {
                ...kafka details....
        }

}
filter {
        json {
                source => "message"
                remove_field => ["message"]
        }
}
output {

        elasticsearch {
                hosts => "http://node1:9050"
                index => "kafkajmx2"
                template_name => "kafka_mbeans"
        }

}

它仍然没有使用模板,但这至少正确地解析了json ...所以现在我明白了:

  {
    "_index": "kafkajmx2",
    "_type": "logs",
    "_id": "AVo4a2Hzj-lM6k7wBcMS",
    "_score": 1,
    "_source": {
      "metric_value_number": 0.9967205071482902,
      "path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf",
      "@timestamp": "2017-02-13T16:54:16.701Z",
      "@version": "1",
      "host": "localhost",
      "metric_path": "kafka1.kafka.network:type=SocketServer,name=NetworkProcessorAvgIdlePercent.Value",
      "type": null
    }
  }

2 个答案:

答案 0 :(得分:5)

您需要改变的是非常简单的。首先在json输入中使用kafka编解码器。无需json过滤器,您可以将其删除。

    kafka {
            ...kafka details....
            codec => "json"
    }

然后在您的elasticsearch输出中,您错过了映射类型(下面的参数document_type),这很重要,否则默认为logs(如您所见)并且不会与您的kafka_mbeans映射类型不匹配。此外,您实际上不需要使用模板,因为您的索引已经存在。进行以下修改:

    elasticsearch {
            hosts => "http://node1:9050"
            index => "kafkajmx2"
            document_type => "kafka_mbeans"
    }

答案 1 :(得分:0)

这是在elasticsearch输出上使用the template_name parameter定义的。

elasticsearch {
        hosts         => "http://elasticsearch:9050"
        index         => "kafkajmx2"
        template_name => "kafka_mbeans"
}
但是,有一个警告。如果要开始创建按时打包的索引(例如每周一个索引),则必须执行一些步骤以确保映射保持不变。你有几个选择:

  • 创建一个elasticsearch模板,并定义它以使用glob应用于索引。例如kafkajmx2-*
  • 在输出上使用the template parameter,它指定一个JSON文件,用于定义将与通过该输出创建的所有索引一起使用的映射。