logstash输出到es模式action实践

本文介绍了Logstash的input、filter、output三个模块,重点讲解了output模块配置,特别是针对Elasticsearch的ACTION操作,包括index、delete、update、create等不同模式。文中提供了一个conf示例,并详细解释了各ACTION的含义和应用场景,如全量同步、删除、修改和创建文档。还特别提到了ACTION配置的注意事项,如避免在日志写入场景指定document_id以提高效率,以及doc_as_upsert和upsert的使用限制。最后,文章提及了Logstash模板管理的相关设置及其对Elasticsearch的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

logstash输出es

模块

logstash一共有 input,filter,output 三个模块

配置

conf示例

input {
  jdbc {
    jdbc_connection_string => "jdbc:mysql:/XXX
    jdbc_user => "XXX"
    jdbc_password => "XXX"
    jdbc_driver_library => "/data/logstash-7.1.1/logstash-core/lib/jars/mysql-connector-java-8.0.19.jar"
    jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
    jdbc_paging_enabled => true
    jdbc_page_size => "10000"
    jdbc_default_timezone =>"Asia/Shanghai"
    statement => "select XXX from user  where id >=:sql_last_value limit 10000"
    schedule => "*/5 * * * * *"
    use_column_value => true
    tracking_column => "id"
    tracking_column_type => "numeric"
    last_run_metadata_path => "/data/logstash-7.1.1/bin/prod/user_prod"
  }
}

output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "update"
    doc_as_upsert => "true"
    document_id => "%{id}"
  }
}

output插件配置

output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "update"
    doc_as_upsert => "true"
    document_id => "%{id}"
  }
}

action类型

  • index:新增覆盖
  • delete:通过id删除文档需要指定 document_id
  • update:修改/修改或新增,upsert
  • create:新增不覆盖

action对应文档看 Elasticsearch bulk API documentation

几种 output es 的 action 配置

  1. 全量同步建立索引
output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "index"
    document_id => "%{id}"
  }
}

注意:

  • 若指定 document_id 会先查询该文档,存在更新不存在新增
  • document_id 不指定 index 模式会自动生成 _id
  • 对于日志写入等不存在修改场景的业务库尽量不要指定 document_id 提高写入效率
  • index 模式是默认 action
  1. 删除指定文档
output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "delete"
    document_id => "%{id}"
  }
}
  1. 修改指定文档
output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "update"
    document_id => "%{id}"
  }
}
  1. 修改指定文档若文档不存在则新增
output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "logstash-%{+YYYY.MM.dd}"
    document_id => "%{id}"
    action => "update"
    doc_as_upsert => true
    # 不能同时设置 doc_as_upsert => ture 和 upsert
    # upsert => '{"test":"hello world"}'
  }
}

注意:

  • doc_as_upsert为true,使用input的值做为文档的值
  • scripted_upsert为true,使用script作为文档的值
  • 不能同时设置 doc_as_upsert 为 true 和 upsert 为json字符串
output {
  elasticsearch {
    scripted_upsert => "true"
    script => "ctx._source.message = params.event.get('message')"
  }
}
  • 设置 upsert:Set upsert content for update mode. Create a new document with this parameter as json string if document_id doesn’t exists;为更新模式设置upsert内容。如果document_id不存在,则使用此参数作为json字符串创建新文档
  1. 只新增文档,文档若存在失败
output {
  elasticsearch {
    hosts => ["XXX:9200"]
    user => "XXX"
    password => "XXX"
    index => "XXX_index"
    action => "create"
    document_id => "%{id}"
    manage_template => true
    template => "/data/logstash-7.1.1/bin/temp/xxx.json"
    template_overwrite => "true"
    template_name => "test"
  }
}

注意:

  • template_overwrite 设置为true,模板名字一样的时候,logstash的该模板(template_name)会覆盖es中的该命名模板
  • manage_template打开/关闭模板管理,默认true若为false需要手动预加载模板到es
预加载
curl -ss -XPUT "http://localhost:9200/_template/indexName/" -H 'Content-Type: application/json' -d @"/data/logstash-7.1.1/bin/temp/xxx.json";
  • 如果打开模板管理,template给出模板的路径
  • template_name是在ES中保存模板的名称

命令

nohup sh logstash --path.data=XXX_test -f XXX.conf > XXX.out&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值