logstash-output elasticsearch插件使用

来源:互联网 发布:贵阳广电网络公司地址 编辑:程序博客网 时间:2024/04/29 05:00

                                                 logstash-output  elasticsearch插件使用

output elasticsearch 插件使用是需要template模板,不指定时会用到默认的模板。

一:首先解释下elasticsearch的一些设置参数:

Retry policy 重操作方针

  这个插件利用es bulk API来优化es的输入。这些请求可能经历部分或全部失败。

 

action=>index  #es要执行的动作   index, deletecreateupdate

index:logstash.时间索引到一个文档

delete:根据id删除一个document(这个动作需要一个id)

create:建立一个索引document,如果id存在 动作失败.

update:根据id更新一个document,有一种特殊情况可以upsert--如果document不是已经存在的情况更新document 。参见upsert选项。

A sprintf style string to change the action based on the content of the event. The value %{[foo]} would use the foo field for the action

document_id=>   为索引提供document id ,对重写elasticsearch中相同id词目很有用

document_type=> 事件要被写入的document type,一般要将相似事件写入同一type,可用%{}引用事件type,默认type=log

index=>logstash-%{+YYYY,MM.dd}  事件要被写进的索引,可是动态的用%{foo}语句

hosts=>[127.0.0.0]  ["127.0.0.1:9200","127.0.0.2:9200"] ["https://127.0.0.1:9200"]["https://127.0.0.1:9200/mypath"] 

manage_template=>true  一个默认的es mapping 模板将启用(除非设置为false 用自己的template)

template=>””  有效的filepath   设置自己的template文件路径,不设置就用已有的

template_name=>logstash es内部模板的名字


二:单个模板template时 

output{  elasticsearch{    hosts=>["172.132.12.3:9200"]    action=>"index"    index=>"indextemplate-logstash"    #document_type=>"%{@type}"    document_id=>"ignore"        template=>"/opt/logstash-conf/es-template.json"    template_name=>"es-template.json"    template_overwrite=>true        }  stdout{      codec=>rubydebug  }}

这里指定了自己的本地template: es-template.json.  内容比较简单 
{  "template" : "indextemplate-*",  "settings" : {    "index.refresh_interval" : "5s",    "number_of_replicas":"1",    "number_of_shards":"3"  },  "mappings" : {    "_default_" : {      "_all" : {"enabled" :false, "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" : "analyzed", "omit_norms" : true,            "fielddata" : { "format" : "disabled" },            "fields" : {              "raw" : {"type": "string", "index" : "not_analyzed", "ignore_above" : 20}            }          }        }      } ]    }  }}

三:多个模板template时

logstash写数据到elasticsearch时,指定单个数据模板没有问题,在配置多个数据模板时候就一定要加一个配置项: template_name ,且名字必须全部为小写

output {        if [type] == "log_01" {                elasticsearch {                        cluster => 'elasticsearch'                        hosts=>[“x.x.x.x:9300”]                          index => 'log_01-%{+YYYY-MM-dd}'                        workers => 1                        #document_type=>"%{@type}"                        document_id=>"ignore"                        template => "/opt/logstash-conf/es-template.json"                        template_name => "template_01.json"                        template_overwrite => true                }        }        if [type] == "log_02" {            elasticsearch {                        cluster => 'elasticsearch'                        hosts => [“x.x.x.x:9300”]                        index => 'log_02-%{+YYYY-MM-dd}'                        #document_type=>"%{@type}"                        document_id=>"ignore"                         workers => 1                        template => "/data/logstash/conf/template_02.json"                        template_name => "template_01.json"                      template_overwrite => true                }        }}





1 0
原创粉丝点击