1 資源準(zhǔn)備
使用的虛擬機(jī)或者生成環(huán)境需先配置好JDK和可以訪問使用的elasticsearch服務(wù)痘括。
logstash-6.4.3 CSDN下載地址: https://download.csdn.net/download/qq_15769939/15709817
logstash-6.4.3 官網(wǎng)下載地址: https://www.elastic.co/cn/downloads/past-releases/logstash-6-4-3
使用Logstatsh的版本號(hào)與elasticsearch版本號(hào)需要保持一致
2 創(chuàng)建索引
同步數(shù)據(jù)到es中,前提得要有索引旗闽,這個(gè)需要手動(dòng)先去創(chuàng)建,用來測(cè)試。比如: logstash-test
3 配置同步數(shù)據(jù)
新建 logstash-test
數(shù)據(jù)庫,建立一個(gè) logstash-test
表协饲,用于同步數(shù)據(jù)測(cè)試
-- ----------------------------
-- Table structure for logstash-test
-- ----------------------------
DROP TABLE IF EXISTS `logstash-test`;
CREATE TABLE `logstash-test` (
`id` int(11) NOT NULL COMMENT '主鍵',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名稱',
`age` int(11) NULL DEFAULT NULL COMMENT '年齡',
`score` float NULL DEFAULT NULL COMMENT '得分',
`updated_time` datetime(0) NULL DEFAULT NULL COMMENT '更新時(shí)間',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `logstash-test`(`id`, `name`, `age`, `score`, `updated_time`) VALUES (1, '張三', 12, 101, '2021-03-10 17:20:25');
INSERT INTO `logstash-test`(`id`, `name`, `age`, `score`, `updated_time`) VALUES (2, '李四', 13, 102, '2021-03-10 17:20:25');
3.1 基礎(chǔ)配置
上傳文件到 /opt/module/software
目錄
[root@localhost bin]# cd /opt/module/software/
[root@localhost software]# ll
-rw-r--r--. 1 root root 153936585 3月 9 13:53 logstash-6.4.3.tar.gz.zip
[root@localhost software]# uzip logstash-6.4.3.tar.gz.zip
[root@localhost software]# tar -zxvf logstash-6.4.3.tar.gz -C /usr/local/
3.2 同步配置
[root@localhost software]# /usr/local/logstash-6.4.3
[root@localhost logstash-6.4.3]# mkdir sync
3.2.1 配置數(shù)據(jù)獲取SQL
這里注意,需要將數(shù)據(jù)庫的驅(qū)動(dòng)jar包放在sync目錄下缴川,jar根據(jù)自身所使用的數(shù)據(jù)庫從maven倉庫里面找到拷貝
[root@localhost sync]# cp /opt/module/software/mysql-connector-java-5.1.41.jar .
[root@localhost sync]# vi logstash-test.sql
select id,name,age,score,updated_time from logstash-test where updated_time >= :sql_last_value
3.2.2 配置自定義模板
查詢默認(rèn)的配置
GET http://192.168.51.5:9200/_template/logstash
{
"logstash": {
"order": 0,
"version": 60001,
"index_patterns": [
"logstash-*"
],
"settings": {
"index": {
"refresh_interval": "5s"
}
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"path_match": "message",
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false
}
}
},
{
"string_fields": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
],
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "keyword"
},
"geoip": {
"dynamic": true,
"properties": {
"ip": {
"type": "ip"
},
"location": {
"type": "geo_point"
},
"latitude": {
"type": "half_float"
},
"longitude": {
"type": "half_float"
}
}
}
}
}
},
"aliases": {}
}
}
自定義配置
[root@localhost sync]# vi /usr/local/logstash-6.4.3/sync/logstash-ik.json
{
"order": 0,
"version": 1,
"index_patterns": [
"*"
],
"settings": {
"index": {
"refresh_interval": "5s"
}
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"path_match": "message",
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false
}
}
},
{
"string_fields": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false,
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
],
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "keyword"
},
"geoip": {
"dynamic": true,
"properties": {
"ip": {
"type": "ip"
},
"location": {
"type": "geo_point"
},
"latitude": {
"type": "half_float"
},
"longitude": {
"type": "half_float"
}
}
}
}
}
},
"aliases": {}
}
3.2.3 配置數(shù)據(jù)同步
[root@localhost sync]# vi logstash-test-sync.conf
input {
jdbc {
# 設(shè)置 MySql/MariaDB 數(shù)據(jù)庫url以及數(shù)據(jù)庫名稱
jdbc_connection_string => "jdbc:mysql://192.168.51.4:3306/logstash-test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"
# 用戶名和密碼
jdbc_user => "root"
jdbc_password => "root"
# 數(shù)據(jù)庫驅(qū)動(dòng)所在位置茉稠,可以是絕對(duì)路徑或者相對(duì)路徑
jdbc_driver_library => "/usr/local/logstash-6.4.3/sync/mysql-connector-java-5.1.41.jar"
# 驅(qū)動(dòng)類名
jdbc_driver_class => "com.mysql.jdbc.Driver"
# 開啟分頁
jdbc_paging_enabled => "true"
# 分頁每頁數(shù)量,可以自定義
jdbc_page_size => "1000"
# 執(zhí)行的sql文件路徑
statement_filepath => "/usr/local/logstash-6.4.3/sync/logstash-test.sql"
# 設(shè)置定時(shí)任務(wù)間隔 含義:分把夸、時(shí)而线、天、月恋日、年膀篮,全部為*默認(rèn)含義為每分鐘跑一次任務(wù)
schedule => "* * * * *"
# 索引類型
type => "_doc"
# 是否開啟記錄上次追蹤的結(jié)果,也就是上次更新的時(shí)間岂膳,這個(gè)會(huì)記錄到 last_run_metadata_path 的文件
use_column_value => true
# 記錄上一次追蹤的結(jié)果值
last_run_metadata_path => "/usr/local/logstash-6.4.3/sync/track_time"
# 如果 use_column_value 為true誓竿, 配置本參數(shù),追蹤的 column 名谈截,可以是自增id或者時(shí)間
tracking_column => "updated_time"
# tracking_column 對(duì)應(yīng)字段的類型
tracking_column_type => "timestamp"
# 是否清除 last_run_metadata_path 的記錄筷屡,true則每次都從頭開始查詢所有的數(shù)據(jù)庫記錄
clean_run => false
# 數(shù)據(jù)庫字段名稱大寫轉(zhuǎn)小寫
lowercase_column_names => false
}
}
output {
elasticsearch {
# es地址
hosts => ["192.168.51.5:9200"]
# 同步的索引名
index => "foodie-items"
# 設(shè)置_docID和數(shù)據(jù)相同
document_id => "%{id}"
# document_id => "%{itemId}"
# 定義模板名稱
template_name => "myik"
# 模板所在位置
template => "/usr/local/logstash-6.4.3/sync/logstash-ik.json"
# 重寫模板
template_overwrite => true
# 默認(rèn)為true,false關(guān)閉logstash自動(dòng)管理模板功能,如果自定義模板簸喂,則設(shè)置為false
manage_template => false
}
# 日志輸出
stdout {
codec => json_lines
}
}
3.2.4 注意事項(xiàng)
配置過程中會(huì)出現(xiàn) 分詞配置不成功毙死,analyzer不顯示
解決方案如下:
先將manage_template => true設(shè)置為true
然后 啟動(dòng) logstash 同步template
使用
http://192.168.51.5:9200/_template/myik
在postman中確認(rèn)是否配置成功上傳,http://192.168.51.5:9200
是es的訪問地址喻鳄,如下圖所示扼倘,即是配置成功
將manage_template => false 設(shè)置為false
重啟logstash
3.3 啟動(dòng)服務(wù)
[root@localhost sync]# cd /usr/local/logstash-6.4.3/bin
[root@localhost bin]# ./logstash -f /usr/local/logstash-6.4.3/sync/logstash-db-sync.conf
服務(wù)啟動(dòng)會(huì)有些慢,耐心等待除呵,數(shù)據(jù)同步到es中
4 相關(guān)信息
- 博文不易再菊,辛苦各位猿友點(diǎn)個(gè)關(guān)注和贊,感謝