使用Logstash同步mysql数据到Elasticsearch(亲自踩坑)
warning:
这篇文章距离上次修改已过186天,其中的内容可能已经有所变动。
input {
jdbc {
jdbc_driver_library => "/path/to/mysql-connector-java-x.x.x-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://localhost:3306/database_name"
jdbc_user => "your_username"
jdbc_password => "your_password"
schedule => "* * * * *"
statement => "SELECT id, name, email FROM users"
clean_run => true
record_last_run => true
last_run_metadata_path => "/var/lib/logstash-jdbc-last_run"
use_column_value => true
tracking_column => "id"
tracking_column_type => "numeric"
type => "jdbc"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "jdbc_mysql_index"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
这个配置文件定义了Logstash的输入、过滤和输出。它使用JDBC插件从MySQL数据库读取数据,并通过JSON过滤器进行处理,最后将数据输出到Elasticsearch,并将处理后的数据输出到标准输出(通常是控制台),以JSON行格式编码。注意,你需要替换数据库连接信息、schedule时间和SQL查询语句以适应你的具体需求。
评论已关闭