ES Logstash安装以及数据同步配置

1 资源准备

使用的虚拟机或者生成环境需先配置好JDK和可以访问使用的elasticsearch服务。

logstash-6.4.3 CSDN下载地址: https://download.csdn.net/download/qq_15769939/15709817

logstash-6.4.3 官网下载地址: https://www.elastic.co/cn/downloads/past-releases/logstash-6-4-3

使用Logstatsh的版本号与elasticsearch版本号需要保持一致

2 创建索引

同步数据到es中,前提得要有索引,这个需要手动先去创建,用来测试。比如: logstash-test

在这里插入图片描述

3 配置同步数据

新建 logstash-test 数据库,建立一个 logstash-test 表,用于同步数据测试

-- ----------------------------
-- 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 '更新时间',
  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 基础配置

上传文件到 /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 配置数据获取SQL

这里注意,需要将数据库的驱动jar包放在sync目录下,jar根据自身所使用的数据库从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 配置自定义模板

查询默认的配置

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": {
    
    }
    }
}

自定义配置

扫描二维码关注公众号,回复: 12927448 查看本文章
[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 配置数据同步

[root@localhost sync]# vi logstash-test-sync.conf
input {
    
    
    jdbc {
    
    
        # 设置 MySql/MariaDB 数据库url以及数据库名称
        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"
        # 数据库驱动所在位置,可以是绝对路径或者相对路径
        jdbc_driver_library => "/usr/local/logstash-6.4.3/sync/mysql-connector-java-5.1.41.jar"
        # 驱动类名
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        # 开启分页
        jdbc_paging_enabled => "true"
        # 分页每页数量,可以自定义
        jdbc_page_size => "1000"
        # 执行的sql文件路径
        statement_filepath => "/usr/local/logstash-6.4.3/sync/logstash-test.sql"
        # 设置定时任务间隔  含义:分、时、天、月、年,全部为*默认含义为每分钟跑一次任务
        schedule => "* * * * *"
        # 索引类型
        type => "_doc"
        # 是否开启记录上次追踪的结果,也就是上次更新的时间,这个会记录到 last_run_metadata_path 的文件
        use_column_value => true
        # 记录上一次追踪的结果值
        last_run_metadata_path => "/usr/local/logstash-6.4.3/sync/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.51.5:9200"]
        # 同步的索引名
        index => "foodie-items"
        # 设置_docID和数据相同
        document_id => "%{
    
    id}"
        # document_id => "%{itemId}"

	# 定义模板名称
	template_name => "myik"
	# 模板所在位置
	template => "/usr/local/logstash-6.4.3/sync/logstash-ik.json"
	# 重写模板
	template_overwrite => true
	# 默认为true,false关闭logstash自动管理模板功能,如果自定义模板,则设置为false
	manage_template => false
    }
    # 日志输出
    stdout {
    
    
        codec => json_lines
    }
}

3.2.4 注意事项

配置过程中会出现 分词配置不成功,analyzer不显示

解决方案如下:

  • 先将manage_template => true设置为true

  • 然后 启动 logstash 同步template

  • 使用http://192.168.51.5:9200/_template/myik在postman中确认是否配置成功上传,http://192.168.51.5:9200是es的访问地址,如下图所示,即是配置成功

在这里插入图片描述

  • 将manage_template => false 设置为false

  • 重启logstash

3.3 启动服务

[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 

服务启动会有些慢,耐心等待,数据同步到es中

4 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/115023530