大数据-188 Logstash Output 插件实战

1. Output 插件概述

Output 是 Logstash 管道的最后阶段,负责将处理后的数据输出到目标系统。

2. stdout 输出

2.1 rubydebug codec

output {
  stdout {
    codec => rubydebug
  }
}

2.2 用途

  • 联调验数
  • 本地开发调试

3. file 输出

3.1 基本配置

output {
  file {
    path => "/var/log/logstash/%{type}-%{+YYYY.MM.dd}.log"
  }
}

3.2 动态路径模板

变量说明
%{+YYYY.MM.dd}时间格式
%{type}事件类型
%{host}主机名

4. Elasticsearch 输出

4.1 基本配置

output {
  elasticsearch {
    hosts => ["es1:9200", "es2:9200"]
    index => "logstash-%{type}-%{+YYYY.MM.dd}"
  }
}

4.2 批量提交

elasticsearch {
  hosts => ["es:9200"]
  manage_template => false
  index => "my-index"
  document_type => "_doc"
}

4.3 重试机制

  • 默认重试 3 次
  • 可配置 retry_max_items

5. 高级特性

5.1 负载均衡

output {
  elasticsearch {
    hosts => ["es1:9200", "es2:9200", "es3:9200"]
  }
}

5.2 条件路由

output {
  if [type] == "nginx" {
    elasticsearch {
      hosts => ["es:9200"]
      index => "nginx-%{+YYYY.MM.dd}"
    }
  } else if [type] == "app" {
    elasticsearch {
      hosts => ["es:9200"]
      index => "app-%{+YYYY.MM.dd}"
    }
  }
}

5.3 多输出并行

output {
  stdout { codec => rubydebug }
  file {
    path => "/var/log/archive/%{+YYYY.MM.dd}.log"
  }
}

6. 常见问题

问题原因解决方案
数据写入慢bulk size 小调大 flush_size
写入失败ES 不可达检查网络/ES 状态
索引模板冲突manage_template=true设为 false

7. 总结

  • stdout 用于调试
  • file 用于归档
  • Elasticsearch 用于检索分析
  • 条件路由实现数据分流