请参见下面的logstash配置文件,以使用jdbc插件将记录从mysql数据库中提取到Elasticsearch索引中。如何修改它,以便基于数据库中的company_id
值生成单独的索引,例如:company_%{company_id}_user_events
。
这可以动态完成吗,还是需要为每个公司ID创建预先配置和硬编码的单独的logstash配置文件?是否有脚本或模板之类的中间立场?
如果有帮助,可以将company_id
字段添加到数据库的ahoy_events
表中,而不是像现在这样通过用户关联“添加”。
当前logstash.conf
input {
jdbc {
jdbc_driver_library => "/opt/mysql-connector-java-5.1.47-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://mysql:3306/${DB_NAME}"
jdbc_user => "${DB_USER}"
jdbc_password => "${DB_PASSWORD}"
schedule => "* * * * *"
statement => "select * from ahoy_events where time > :sql_last_value"
}
}
filter {
jdbc_streaming {
jdbc_driver_library => "/opt/mysql-connector-java-5.1.47-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://mysql:3306/${DB_NAME}"
jdbc_user => "${DB_USER}"
jdbc_password => "${DB_PASSWORD}"
statement => "select * from users where id = :user"
parameters => { "user" => "user_id" }
target => "user"
}
jdbc_streaming {
jdbc_driver_library => "/opt/mysql-connector-java-5.1.47-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://mysql:3306/${DB_NAME}"
jdbc_user => "${DB_USER}"
jdbc_password => "${DB_PASSWORD}"
statement => "select * from visits where id = :visits"
parameters => { "visits" => "visit_id" }
target => "visits"
}
mutate {
add_field => { "company_id" => "%{[user][0][company_id]}"}
}
jdbc_streaming {
jdbc_driver_library => "/opt/mysql-connector-java-5.1.47-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://mysql:3306/${DB_NAME}"
jdbc_user => "${DB_USER}"
jdbc_password => "${DB_PASSWORD}"
statement => "select * from companies where id = :company_id"
parameters => { "company_id" => "company_id" }
target => "company"
}
json {
source => "properties"
target => "properties"
}
mutate {
add_field => { "user_name" => "%{[user][0][name]}" }
}
mutate {
add_field => { "company_name" => "%{[company][0][name]}" }
}
mutate {
rename => { "[visits][0]" => "visit" }
}
mutate {
remove_field => ["visits", "company", "user"]
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "user_events-%{+YYYY.MM.dd}"
document_id => "%{id}"
}
}
所需结果是一个索引,该索引的名称空间为company_id:
company_%{company_id}_user_events
这样我以后可以以相同的模式添加其他索引
company_%{company_id}_other_records
答案 0 :(得分:0)
不确定100%,但是从技术上讲应该如此简单:
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "company_%{company_id}_events-%{+YYYY.MM.dd}"
document_id => "%{id}"
}
}