ElasticSearch——Logstash输出到Elasticsearch配置

时间:2022-08-28 10:29:11

位置

在Logstash的.conf配置文件中的output中配置ElasticSearch

示例:

output {
  elasticsearch{
action => "index"
index => "%{[fields][product_type]}-transaction-%{+YYYY-MM}"
  hosts => ["10.0.xx.xx:9200", "10.0.xx.xx:9200", "10.0.xx.xx:9200"]
}
}

action

  • index 给一个文档建立索引
  • delete 通过id值删除一个文档(这个action需要指定一个id值)
  • create 插入一条文档信息,如果这条文档信息在索引中已经存在,那么本次插入工作失败
  • update 通过id值更新一个文档。更新有个特殊的案例upsert,如果被更新的文档还不存在,那么就会用到upsert

示例:

action => "index"

index

写入事件所用的索引。可以动态的使用%{foo}语法,它的默认值是:
"logstash-%{+YYYY.MM.dd}",以天为单位分割的索引,使你可以很容易的删除老的数据或者搜索指定时间范围内的数据。

索引不能包含大写字母。推荐使用以周为索引的ISO 8601格式,例如logstash-%{+xxxx.ww}

示例:

index => "%{[fields][product_type]}-transaction-%{+YYYY-MM}"

hosts

是一个数组类型的值

意http协议使用的是http地址,端口是9200,示例:

hosts => ["10.0.xx.xx:9200", "10.0.xx.xx:9200", "10.0.xx.xx:9200"]

document_type

定义es索引的type,一般你应该让同一种类型的日志存到同一种type中,比如debug日志和error日志存到不同的type中

如果不设置默认type为logs

template

如果你愿意,你可以设置指向你自己模板的路径。如果没有设置,那么默认的模板会被使用

template_name

这个配置项用来定义在Elasticsearch中模板的命名

注意删除旧的模板示例:

curl -XDELETE <http://localhost:9200/_template/OldTemplateName?pretty>

template_overwrite

布尔类型 默认为false
设置为true表示如果你有一个自定义的模板叫logstash,那么将会用你自定义模板覆盖默认模板logstash

manage_template

布尔类型 默认为true
设置为false将关闭logstash自动管理模板功能
比如你定义了一个自定义模板,更加字段名动态生成字段,那么应该设置为false

order参数

ELK Stack 在入门学习过程中,必然会碰到自己修改定制索引映射(mapping)乃至模板(template)的问题。
这时候,不少比较认真看 Logstash 文档的新用户会通过下面这段配置来制定自己的模板策略:

output {
elasticsearch {
host => "127.0.0.1"
manage_template => true
template => "/path/to/mytemplate"
template_name => "myname"
}
}

然而随后就发现,自己辛辛苦苦修改出来的模板,通过 curl -XGET 'http://127.0.0.1:9200/_template/myname' 看也确实上传成功了,但实际新数据索引创建出来,就是没生效!

这个原因是:Logstash 默认会上传一个名叫 logstash 的模板到 ES 里。如果你在使用上面这个配置之前,曾经运行过 Logstash(一般来说都会),那么 ES 里就已经存在这么一个模板了。你可以curl -XGET 'http://127.0.0.1:9200/_template/logstash' 验证。

这个时候,ES 里就变成有两个模板,logstash 和 myname,都匹配 logstash-* 索引名,要求设置一定的映射规则了。

ES 会按照一定的规则来尝试自动 merge 多个都匹配上了的模板规则,最终运用到索引上

其中要点就是:template 是可以设置 order 参数的!而不写这个参数,默认的 order 值就是 0。order 值越大,在 merge 规则的时候优先级越高。

所以,解决这个问题的办法很简单:在你自定义的 template 里,加一行,变成这样:

{
"template" : "logstash-*",
"order" : ,
"settings" : { ... },
"mappings" : { ... }
}

当然,其实如果只从 Logstash 配置角度出发,其实更简单的办法是:直接修改原来默认的 logstash 模板,然后模板名称也不要改,就好了:

output {
elasticsearch {
host => "127.0.0.1"
manage_template => true
template_overwrite => true
}
}

为elasticsearch配置模板

在使用logstash收集日志的时候,我们一般会使用logstash自带的动态索引模板,虽然无须我们做任何定制操作,就能把我们的日志数据推送到elasticsearch索引集群中

但是在我们查询的时候,就会发现,默认的索引模板常常把我们不需要分词的字段,给分词了,这样以来,我们的比较重要的聚合统计就不准确了:

所以这时候,就需要我们自定义一些索引模板了

在logstash与elasticsearch集成的时候,总共有如下几种使用模板的方式:

  1. 使用默认自带的索引模板 ,大部分的字段都会分词,适合开发和时候快速验证使用

  2. 在logstash收集端自定义配置模板,因为分散在收集机器上,维护比较麻烦

  3. 在elasticsearc服务端自定义配置模板,由elasticsearch负责加载模板,可动态更改,全局生效,维护比较容易

使用默认自带的索引模板

ElasticSearch默认自带了一个名字为”logstash”的模板,默认应用于Logstash写入数据到ElasticSearch使用

优点:最简单,无须任何配置

缺点:无法自定义一些配置,例如:分词方式

在logstash收集端自定义配置模板

使用第二种,适合小规模集群的日志收集

需要在logstash的output插件中使用template指定本机器上的一个模板json路径, 例如 template => "/tmp/logstash.json"

优点:配置简单

缺点:因为分散在Logstash Indexer机器上,维护起来比较麻烦

在elasticsearc服务端自定义配置模板

manage_template => false//关闭logstash自动管理模板功能
template_name => "xxx"//映射模板的名字

第三种需要在elasticsearch的集群中的config/templates路径下配置模板json,在elasticsearch中索引模板可分为两种

静态模板

适合索引字段数据固定的场景,一旦配置完成,不能向里面加入多余的字段,否则会报错

优点:scheam已知,业务场景明确,不容易出现因字段随便映射从而造成元数据撑爆es内存,从而导致es集群全部宕机,维护比较容易,可动态更改,全局生效。

缺点:字段数多的情况下配置稍繁琐

一个静态索引模板配置例子如下:

{
"xxx" : {
"template": "xxx-*",
"settings": {
"index.number_of_shards": ,
"number_of_replicas":
},
"mappings" : {
"logs" : {
"properties" : {
"@timestamp" : { //这是专门给kibana用的一个字段,时间索引
"type" : "date",
"format" : "dateOptionalTime",
"doc_values" : true
},
"@version" : {
"type" : "string",
"index" : "not_analyzed",
"doc_values" : true
},
"id" : {
"type" : "string",
"index" : "not_analyzed"
},
"name" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
}

动态模板

适合字段数不明确,大量字段的配置类型相同的场景,多加字段不会报错

优点:可动态添加任意字段,无须改动scheaml,

缺点:如果添加的字段非常多,有可能造成es集群宕机

一个动态索引模板配置例子如下:

{
"template" : "xxx-*",
"settings" : {
"index.number_of_shards": ,
"number_of_replicas": },
"mappings" : {
"_default_" : {
"_all" : {"enabled" : true, "omit_norms" : true},
"dynamic_templates" : [ {
"message_field" : {
"match" : "message",
"match_mapping_type" : "string",
"mapping" : {
"type" : "string", "index" : "analyzed", "omit_norms" : true,
"fielddata" : { "format" : "disabled" }
}
}
}, {
"string_fields" : {
"match" : "*",
"match_mapping_type" : "string",
"mapping" : {
"type" : "string", "index" : "not_analyzed", "doc_values" : true
}
}
} ],
"properties" : {
"@timestamp": { "type": "date" },
"@version": { "type": "string", "index": "not_analyzed" },
"geoip" : {
"dynamic": true,
"properties" : {
"ip": { "type": "ip" },
"location" : { "type" : "geo_point" },
"latitude" : { "type" : "float" },
"longitude" : { "type" : "float" }
}
}
}
}
}
}

只设置message字段分词,其他的字段默认都不分词

模板结构

  • 通用设置 主要是模板匹配索引的过滤规则,影响该模板对哪些索引生效
  • settings:配置索引的公共参数,比如索引的replicas,以及分片数shards等参数
  • mappings:最重要的一部分,在这部分中配置每个type下的每个field的相关属性,比如field类型(string,long,date等等),是否分词,是否在内存中缓存等等属性都在这部分配置
  • aliases:索引别名,索引别名可用在索引数据迁移等用途上。

例子:

{
"logstash" : {
"order" : ,
"template" : "logstash-*",
"settings" : {
"index" : {
"refresh_interval" : "5s"
}
},
"mappings" : {
"_default_" : {
"dynamic_templates" : [ {
"message_field" : {
"mapping" : {
"fielddata" : {
"format" : "disabled"
},
"index" : "analyzed",
"omit_norms" : true,
"type" : "string"
},
"match_mapping_type" : "string",
"match" : "message"
}
}, {
"string_fields" : {
"mapping" : {
"fielddata" : {
"format" : "disabled"
},
"index" : "analyzed",
"omit_norms" : true,
"type" : "string",
"fields" : {
"raw" : {
"ignore_above" : ,
"index" : "not_analyzed",
"type" : "string"
}
}
},
"match_mapping_type" : "string",
"match" : "*"
}
} ],
"_all" : {
"omit_norms" : true,
"enabled" : true
},
"properties" : {
"@timestamp" : {
"type" : "date"
},
"geoip" : {
"dynamic" : true,
"properties" : {
"ip" : {
"type" : "ip"
},
"latitude" : {
"type" : "float"
},
"location" : {
"type" : "geo_point"
},
"longitude" : {
"type" : "float"
}
}
},
"@version" : {
"index" : "not_analyzed",
"type" : "string"
}
}
}
},
"aliases" : { }
}
}
我们创建一个自定义Template动态模板,这个模板指定匹配所有以”go_logsindex“开始的索引,并且指定允许添加新字段,匹配所有string类型的新字段会创建一个raw的嵌套字段,这个raw嵌套字段类型也是string,但是是not_analyzed不分词的(主要用于解决一些analyzed的string字段无法做统计,但可以使用这个raw嵌套字段做统计)
{
"template": "go_logs_index_*",
"order":,
"settings": {
"index.number_of_replicas": "",
"index.number_of_shards": "",
"index.refresh_interval" : "10s"
},
"mappings": {
"_default_": {
"_all": {
"enabled": false
},
"dynamic_templates": [
{
"my_template": {
"match_mapping_type": "string",
"mapping": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
]
},
"go": {
"properties": {
"timestamp": {
"type": "string",
"index": "not_analyzed"
},
"msg": {
"type": "string",
"analyzer": "ik",
"search_analyzer": "ik_smart"
},
"file": {
"type": "string",
"index": "not_analyzed"
},
"line": {
"type": "string",
"index": "not_analyzed"
},
"threadid": {
"type": "string",
"index": "not_analyzed"
},
"info": {
"type": "string",
"index": "not_analyzed"
},
"type": {
"type": "string",
"index": "not_analyzed"
},
"@timestamp": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"@version": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

总结

第三种方式统一管理Template最好,推荐使用第三种方式,但是具体问题具体分析。例如场景是Logstash 和ElasticSearch都在一台服务器,第二种就比较好。

定制索引模板,是搜索业务中一项比较重要的步骤,需要注意的地方有很多,比如:
1.字段数固定吗
2.字段类型是什么
3.分不分词
4.索引不索引
5.存储不存储
6.排不排序
7.是否加权

参考链接:https://www.jianshu.com/p/0b89c07021f4

ElasticSearch——Logstash输出到Elasticsearch配置的更多相关文章

  1. ELk&lpar;Elasticsearch&comma; Logstash&comma; Kibana&rpar;的安装配置

    目录 ELk(Elasticsearch, Logstash, Kibana)的安装配置 1. Elasticsearch的安装-官网 2. Kibana的安装配置-官网 3. Logstash的安装 ...

  2. logstash输出到elasticsearch多索引

    目标:将json格式的两类日志输出到elasticsearch两类索引 1. 安装logstash. 2. 编写logstash处理配置文件,创建一个test.conf文件,内容如下: input { ...

  3. logstash输出至elasticsearch

    续上一篇 上一篇描述了通过logback配置用logstash收集springmvc项目日志,本文是描述如何进一步通过elasticsearch对所收集数据进行的分析. output { elasti ...

  4. ELK系列五:Logstash输出到Elasticsearch和redis

    1.Logstash与Redis的读写 1.1 Logstash 写入Redis 看完Logstash的输入,想必大家都清楚了Logstash的基本用法,那就是写配置文件. output{ { red ...

  5. logstash 输出到elasticsearch 自动建立index

    由于es 单index 所能承受的数据量有限,之前情况是到400w数据300G左右的时候,整个数据的插入会变得特别慢(索引重建)甚至会导致集群之间的通信断开,于是我们采用每天一个index的方法来缓解 ...

  6. CentOS 6&period;x ELK&lpar;Elasticsearch&plus;Logstash&plus;Kibana&rpar;

    CentOS 6.x ELK(Elasticsearch+Logstash+Kibana) 前言 Elasticsearch + Logstash + Kibana(ELK)是一套开源的日志管理方案, ...

  7. Elasticsearch &plus; logstash &plus; kibana 配置

    Elasticsearch 配置 Elasticsearch不仅仅是Lucene和全文搜索,我们还能这样去描述它: 分布式的实时文件存储,每个字段都被索引并可被搜索 分布式的实时分析搜索引擎 可以扩展 ...

  8. logstash收集的日志输出到elasticsearch中

    logstash收集的日志输出到elasticsearch中 一.需求 二.实现步骤 1.编写pipeline文件 1.`elasticsearch`配置参数解析: 2.可能会报的一个异常 2.准备测 ...

  9. ELK 架构之 Elasticsearch、Kibana、Logstash 和 Filebeat 安装配置汇总(6&period;2&period;4 版本)

    相关文章: ELK 架构之 Elasticsearch 和 Kibana 安装配置 ELK 架构之 Logstash 和 Filebeat 安装配置 ELK 架构之 Logstash 和 Filebe ...

随机推荐

  1. &lbrack;转&rsqb;jquery 点击表格变为input可以修改无刷新更新数据

    原文地址:http://www.freejs.net/article_biaodan_43.html 之前已经发了2篇类似的文章<点击变td为input更新>和<jquery表格可编 ...

  2. Caffe学习系列&lpar;5&rpar;:其它常用层及参数

    本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层及其它们的参数配置. 1.softmax-loss so ...

  3. 6、Android中的NFC技术

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...

  4. oracle PL&sol;SQL(procedure language&sol;SQL)程序设计--控制结构(if else )

    IF逻辑结构:IF-THEN-END IFIF-THEN-ELSE-END IFIF-THEN-ELSIF-END IF 语法 IF condition THEN  statements;[ELSIF ...

  5. 如何将android studio项目转换成eclipse

    更新:虽然本人坚守eclipse很久,但是现在我也不再推荐继续用eclispe了,自己的项目用还没什么问题,但是你如果想用github上的项目,用eclispe会越来越难.如果你仍然感兴趣,继续看下面 ...

  6. uml系列图(一)——与uml的第一次约会

    uml视频终于开始看了,再看之前先大概了解了一下uml都有啥. 老规矩,有图有真相: 暂时的理解就这么多,等到uml看完的时候总结跟现在这张图比一下,应该是有很大的区别吧. uml是一种可视化的建模语 ...

  7. nio、bio区别,应运场景

    bio阻塞i/o a.面向流的,InputStream(),OuputStream字节输入流,字节输出流,Reader,Writer字符输入流,字符输出流 b.阻塞的IO,比如Socket,它的底层用 ...

  8. 毕业不到一年,绩效打了个D!

    周末了,和大家来聊聊程序员工作态度的问题. 说说栈长的事迹吧,这是好多年前的事了,那时候,栈长才毕业不到一年,那次绩效打了个D!事后,我很气愤啊,我那时还在博客上写文章怒骂了部门经理,现在想起来,真是 ...

  9. Dockerfile中ENTRYPOINT 和 CMD的区别

    一.dockerfile中的 CMD 1.每个dockerfile中只能有一个CMD如果有多个那么只执行最后一个. 2.CMD 相当于启动docker时候后面添加的参数看,举个简单例子: docker ...

  10. C&num;版Aliyun DNS API

    阿里云解析API,是为域名开发者.注册商.域名代理商等提供的开放和便捷的解析服务接口.API依托于万网云解析服务,可以方便的管理域名和解析记录,让你的解析管理变的随心省时*舒畅. 一.先附上Aliy ...