一、安装
1、下载
官网下载,版本与es保持一致
将文件上传服务器并解压
我解压后的文件路径
/usr/local/elasticsearch/logstash-7.6.2
2、配置通过文件
- 在logstash文件夹下创建同步文件夹
名字自取
mkdir dbsync
- 在新建的
dbsync
文件夹中创建数据同步.conf
文件
vim logstash-db-sync.conf
- 将数据库驱动文件添加到当前文件夹下
mv /root/software/mysql-connector-java-8.0.22.jar ./
- 添加sql执行文件
同步数据的sql文件
food-items.sql
- 创建中文分词规则
通过以下方式可以获取到基本的json样式稍作修改即可
GET 192.168.56.109:9200/_template/logstash
logstash-ik.json
3、food-items.sql
配置信息
根据实际情况编写
SELECT
i.id as itemId,
i.item_name as itemName,
i.sell_counts as sellCounts,
ii.url as imgUrl,
tempSpec.price_discount as price,
i.updated_time as updated_time
FROM
items i
LEFT JOIN
items_img ii
on
i.id = ii.item_id
LEFT JOIN
(SELECT item_id,MIN(price_discount) as price_discount from items_spec GROUP BY item_id) tempSpec
on
i.id = tempSpec.item_id
WHERE
ii.is_main = 1
and
i.updated_time >= :sql_last_value
4、logstash-ik.json
{
"order": 0,
"version": 1,
"index_patterns": [
"*"
],
"settings": {
"index": {
"number_of_shards": "1",
"refresh_interval": "5s"
}
},
"mappings": {
"dynamic_templates": [
{
"message_field": {
"path_match": "message",
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string"
}
},
{
"string_fields": {
"mapping": {
"norms": false,
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"@timestamp": {
"type": "date"
},
"geoip": {
"dynamic": true,
"properties": {
"ip": {
"type": "ip"
},
"latitude": {
"type": "half_float"
},
"location": {
"type": "geo_point"
},
"longitude": {
"type": "half_float"
}
}
},
"@version": {
"type": "keyword"
}
}
},
"aliases": {}
}
3、logstash-db-sync.conf
配置信息
根据实际情况配置
input {
jdbc {
# 设置 MySql/MariaDB 数据库url以及数据库名称
jdbc_connection_string => "jdbc:mysql://192.168.56.112:3306/food?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai"
# 用户名和密码
jdbc_user => "root"
jdbc_password => "root123"
# 数据库驱动所在位置,可以是绝对路径或者相对路径
jdbc_driver_library => "/usr/local/elasticsearch/logstash-7.6.2/dbsync/mysql-connector-java-8.0.22.jar"
# 驱动类名
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
# 开启分页
jdbc_paging_enabled => "true"
# 分页每页数量,可以自定义
jdbc_page_size => "10000"
# 执行的sql文件路径
statement_filepath => "/usr/local/elasticsearch/logstash-7.6.2/dbsync/food-items.sql"
# 设置定时任务间隔 含义:分、时、天、月、年,全部为*默认含义为每分钟跑一次任务
schedule => "* * * * *"
# 索引类型
type => "_doc"
# 是否开启记录上次追踪的结果,也就是上次更新的时间,这个会记录到 last_run_metadata_path 的文件
use_column_value => true
# 记录上一次追踪的结果值
last_run_metadata_path => "/usr/local/elasticsearch/logstash-7.6.2/dbsync/track_time"
# 如果 use_column_value 为true, 配置本参数,追踪的 column 名,可以是自增id或者时间
tracking_column => "updated_time"
# tracking_column 对应字段的类型
tracking_column_type => "timestamp"
# 是否清除 last_run_metadata_path 的记录,true则每次都从头开始查询所有的数据库记录
clean_run => false
# 数据库字段名称大写转小写
lowercase_column_names => false
}
}
output {
elasticsearch {
# es地址
hosts => ["192.168.56.109:9200"]
# 用户名
user => "elastic"
# 密码
password => "root123"
# 同步的索引名
index => "food-items"
# 设置_docID和数据相同
document_id => "%{itemId}"
# 定义模板名称
template_name => "ik-word"
# 模板所在位置
template => "/usr/local/elasticsearch/logstash-7.6.2/dbsync/logstash-ik.json"
# 重写模板
template_overwrite => true
# 默认为true,false关闭logstash自动管理模板功能,如果自定义模板,则设置为false
manage_template => false
}
# 日志输出
stdout {
codec => json_lines
}
}
6、启动logstash
进入目录:/usr/local/elasticsearch/logstash-7.6.2/bin
./logstash -f /usr/local/elasticsearch/logstash-7.6.2/dbsync/logstash-db-sync.conf