《果然新鲜》电商项目(44)- 利用Logstash自动同步数据库内容到ES

Posted IT刘老师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《果然新鲜》电商项目(44)- 利用Logstash自动同步数据库内容到ES相关的知识,希望对你有一定的参考价值。

文章目录

1. 如何实现数据库与ES同步

我们都知道ES是搜索引擎,它的搜索速度比数据库快很多,那如何将数据库里面的内容同步到ES呢?这个时候就需要用到中间件Logstash了。

Logstash是一个开源数据收集引擎,具有实时管道功能,Logstash可以动态地将来自不同数据源的数据统一起来,并将数据标准化到你所选择的目的地。

我自己画了一张数据库与ES同步的原理图,如下:

首先在Logstash里配置相关的信息,如下:

1.配置mysql信息有:

数据库连接信息
数据库查询最新数据SQL

2.配置定时器:

配置定时器的定时规则(如:每分钟从数据库里定时一次)

3.配置ES:

配置ES连接信息
ES文档id等信息

2.数据库数据录入

数据库的表设计已经在前面的《商品搜索服务数据库设计》已经讲解过了,为了本文有演示效果和以后的博客讲解,先插入假数据

category表:

INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (1, 0, '新鲜水果', 0, 0, NULL, NULL, '2020-03-02 15:00:57', '2020-03-02 15:00:57', '2020-03-02 15:00:57');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (2, 1, '热销水果', 0, 0, NULL, NULL, '2020-03-02 15:02:08', '2020-03-02 15:02:08', '2020-03-02 15:02:08');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (3, 1, '时令水果', 0, 0, NULL, NULL, '2020-03-02 15:02:08', '2020-03-02 15:02:08', '2020-03-02 15:02:08');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (4, 1, '热带水果', 0, 0, NULL, NULL, '2020-03-02 15:02:08', '2020-03-02 15:02:08', '2020-03-02 15:02:08');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (5, 1, '地标水果', 0, 0, NULL, NULL, '2020-03-02 15:02:08', '2020-03-02 15:02:08', '2020-03-02 15:02:08');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (6, 2, '苹果', 0, 0, NULL, NULL, '2020-03-02 15:03:19', '2020-03-02 15:03:19', '2020-03-02 15:03:19');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (7, 2, '橙子', 0, 0, NULL, NULL, '2020-03-02 15:03:19', '2020-03-02 15:03:19', '2020-03-02 15:03:19');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (8, 0, '海鲜水产', 0, 0, NULL, NULL, '2020-03-02 15:28:39', '2020-03-02 15:28:39', '2020-03-02 15:28:39');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (9, 8, '鱼类', 0, 0, NULL, NULL, '2020-03-02 15:28:49', '2020-03-02 15:28:49', '2020-03-02 15:28:49');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (10, 8, '虾类', 0, 0, NULL, NULL, '2020-03-02 15:28:49', '2020-03-02 15:28:49', '2020-03-02 15:28:49');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (11, 9, '三文鱼', 0, 0, NULL, NULL, '2020-03-02 15:30:10', '2020-03-02 15:30:10', '2020-03-02 15:30:10');
INSERT INTO `guoranxinxian-goods`.`category`(`ID`, `PARENT_ID`, `NAME`, `STATUS`, `SORT_ORDER`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (12, 9, '鳕鱼', 0, 0, NULL, NULL, '2020-03-02 15:30:10', '2020-03-02 15:30:10', '2020-03-02 15:30:10');

attribute_key表:

INSERT INTO `guoranxinxian-goods`.`attribute_key`(`ID`, `CATEGORY_ID`, `ATTRIBUTE_NAME`, `NAME_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (1, 6, '重量', '0', 1, NULL, '2020-03-02 15:34:35', '2020-03-02 15:34:35', '2020-03-02 15:34:35');
INSERT INTO `guoranxinxian-goods`.`attribute_key`(`ID`, `CATEGORY_ID`, `ATTRIBUTE_NAME`, `NAME_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (2, 6, '类别', '0', 1, NULL, '2020-03-02 15:34:35', '2020-03-02 15:34:35', '2020-03-02 15:34:35');
INSERT INTO `guoranxinxian-goods`.`attribute_key`(`ID`, `CATEGORY_ID`, `ATTRIBUTE_NAME`, `NAME_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (3, 6, '原产地', '0', 1, NULL, '2020-03-02 15:34:35', '2020-03-02 15:34:35', '2020-03-02 15:34:35');
INSERT INTO `guoranxinxian-goods`.`attribute_key`(`ID`, `CATEGORY_ID`, `ATTRIBUTE_NAME`, `NAME_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (4, 6, '售卖方式', '0', 1, NULL, '2020-03-02 15:34:35', '2020-03-02 15:34:35', '2020-03-02 15:34:35');

attribute_vallue表:

INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (1, '1', '3kg-4kg', '0', 1, NULL, '2020-03-02 15:36:27', '2020-03-02 15:36:27', '2020-03-02 15:36:27');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (2, '1', '1000g以下', '0', 1, NULL, '2020-03-02 15:36:42', '2020-03-02 15:36:42', '2020-03-02 15:36:42');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (3, '1', '1000-1999g', '0', 1, NULL, '2020-03-02 15:36:43', '2020-03-02 15:36:43', '2020-03-02 15:36:43');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (4, '1', '2000-3999g', '0', 1, NULL, '2020-03-02 15:36:43', '2020-03-02 15:36:43', '2020-03-02 15:36:43');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (5, '2', '红富士', '0', 1, NULL, '2020-03-02 15:38:55', '2020-03-02 15:38:55', '2020-03-02 15:38:55');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (6, '2', '雪莲果', '0', 1, NULL, '2020-03-02 15:38:55', '2020-03-02 15:38:55', '2020-03-02 15:38:55');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, `VALUE_SORT`, `REVISION`, `CREATED_BY`, `CREATED_TIME`, `UPDATED_BY`, `UPDATED_TIME`) VALUES (7, '2', '徐香', '0', 1, NULL, '2020-03-02 15:38:55', '2020-03-02 15:38:55', '2020-03-02 15:38:55');
INSERT INTO `guoranxinxian-goods`.`attribute_value`(`ID`, `ATTRIBUTE_ID`, `ATTRIBUTE_VALUE`, 《果然新鲜》电商项目(45)- 利用Logstash自动同步数据库内容到ES(多文件方式)

《果然新鲜》电商项目(27)- 门户界面

《果然新鲜》电商项目(23)- 全局异常捕获

《果然新鲜》电商项目(22)- DTO接口细分

《果然新鲜》电商项目(24)- 日志打印

《果然新鲜》电商项目(46)- MQ与Logstash实现数据库同步到ES的区别