索引的創(chuàng)建以及IK的安裝詳見:elasticsearch與mysql數(shù)據(jù)同步(go-mysql-elasticsearch)
1. Logstash介紹
Logstash是免費且開放的服務器端數(shù)據(jù)處理管道叮称,能夠從多個來源采集數(shù)據(jù),轉(zhuǎn)換數(shù)據(jù),然后將數(shù)據(jù)發(fā)送到您最喜歡的“存儲庫”中押袍。
Logstash 是一個功能強大的工具朽缎,可與各種部署集成阐斜。 它提供了大量插件名惩,可幫助你解析蛮拔,豐富碟摆,轉(zhuǎn)換和緩沖來自各種來源的數(shù)據(jù)晃财。 如果你的數(shù)據(jù)需要 Beats 中沒有的其他處理,則需要將 Logstash 添加到部署中典蜕。
2. 應用場景
1.日志搜索器:logstash采集断盛、處理、轉(zhuǎn)發(fā)到elasticsearch存儲愉舔,在kibana進行展示
2.Elk日志分析(elasticsearch+logstash+kibana)
3.logstash同步mysql數(shù)據(jù)庫數(shù)據(jù)到es
3. logstash安裝
1.拉取logstash鏡像(需要與es版本對應)
docker pull logstash:7.12.1
2.構建logstash容器
#創(chuàng)建一個用于存儲logstash配置以及插件的目錄
mkdir /docker/logstash
docker run -p 9900:9900 -d --name logstash -v /docker/logstash:/etc/logstash/pipeline --privileged=true logstash:7.12.1
3.進入容器內(nèi)部安裝jdbc和elasticsearch插件
#進入容器內(nèi)部
docker exec -it logstash bash
#使用logstash-plugin安裝器安裝logstash-input-jdbc插件钢猛,改安裝器在bin目錄下(此插件鏡像新版本自帶)
logstash-plugin install logstash-input-jdbc
#安裝數(shù)據(jù)輸出到es的插件
logstash-plugin install logstash-output-elasticsearch
4.下載jdbc的mysql-connection.jar包
https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.24/mysql-connector-java-8.0.24.jar
5.修改容器內(nèi)部配置
修改/usr/share/logstash/config/logstash.yml文件
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://172.17.0.7:9200" ]
修改/usr/share/logstash/config/pipelines.yml文件
- pipeline.id: table1
path.config: "/etc/logstash/pipeline/logstash.conf"
6.退出容器,創(chuàng)建配置文件以及相關配置信息
創(chuàng)建配置文件
touch /docker/logstash/logstash.conf
配置文件內(nèi)容(全量的配置文件)
input {
stdin { }
jdbc {
#注意mysql連接地址一定要用ip轩缤,不能使用localhost等
jdbc_connection_string => "jdbc:mysql://172.17.0.3:3306/lmrs"
jdbc_user => "root"
jdbc_password => "root"
#這個jar包的地址是容器內(nèi)的地址
jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
statement => "select id,`name`,long_name,brand_id,three_category_id as category_id,shop_id,price,status,sold_count,review_count,create_time,last_time from lmrs_products"
schedule => "* * * * *"
}
}
output {
elasticsearch {
#注意es連接地址一定要用ip命迈,不能使用localhost等
hosts => "172.17.0.7:9200"
index => "products"
document_type => "_doc"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
增量的配置文件
input {
stdin { }
jdbc {
#注意mysql連接地址一定要用ip,不能使用localhost等
jdbc_connection_string => "jdbc:mysql://172.17.0.3:3306/lmrs"
jdbc_user => "root"
jdbc_password => "root"
#數(shù)據(jù)庫重連嘗試
connection_retry_attempts => "3"
#數(shù)據(jù)庫連接可用校驗超時時間火的,默認為3600s
jdbc_validation_timeout => "3600"
#這個jar包的地址是容器內(nèi)的地址
jdbc_driver_library => "/etc/logstash/pipeline/mysql-connector-java-8.0.24.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
#開啟分頁查詢(默認是false)
jdbc_paging_enabled => "true"
#單次分頁查詢條數(shù)(默認100000壶愤,字段較多的話,可以適當調(diào)整這個數(shù)值)
jdbc_page_size => "50000"
#執(zhí)行的sql語句
statement => "SELECT a.id,a.`name`,a.long_name,a.brand_id,a.three_category_id AS category_id,a.shop_id,a.price,a.`status`,a.sold_count,a.review_count,a.create_time,a.last_time FROM lmrs_products AS a where a.id > :sql_last_value"
#需要記錄查詢結果某字段的值時馏鹤,此字段為true征椒,否則默認tracking_colum為timestamp的值
use_column_value => true
#是否將字段名轉(zhuǎn)為小寫,默認為true(如果具備序列化或者反序列化湃累,建議設置為false)
lowercase_column_names => false
#需要記錄的字段勃救,同于增量同步,需要是數(shù)據(jù)庫字段
tracking_column => id
#記錄字段的數(shù)據(jù)類型
tracking_column_type => numeric
#上次數(shù)據(jù)存放位置
record_last_run => true
#上一個sql_last_value的存放路徑脱茉,必須在文件中指定字段的初始值
last_run_metadata_path => "/etc/logstash/pipeline/products.txt"
#是否清除last_run_metadata_path的記錄剪芥,需要增量同步這個字段的值必須為false
clean_run => false
#同步的頻率(分 時 天 月 年)默認為每分鐘同步一次
schedule => "* * * * *"
}
}
output {
elasticsearch {
#注意es連接地址一定要用ip,不能使用localhost等
hosts => "172.17.0.7:9200"
index => "products"
document_type => "_doc"
document_id => "%{id}"
}
stdout {
codec => json_lines
}
}
注:
last_run_metadata_path => "/etc/logstash/pipeline/products.txt":因為需要記錄下上次同步的數(shù)據(jù)id琴许,所以這里會有一個文件進行存儲這個id税肪,需要在logstash目錄下去創(chuàng)建一個txt文件,用于存儲這個id,同時需要給予權限益兄。不給會出現(xiàn)權限異常問題(Permission denied)
7.相關演示
查看索引數(shù)據(jù)
GET /_cat/indices?v
啟動容器锻梳,可通過docker logs -f logstash查看運行日志,稍后查看索引數(shù)據(jù)