项目所用依赖

<modelVersion>4.0.0</modelVersion><artifactId>tm-shop-model</artifactId><dependencies><!--es--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- JWT依赖 --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency><!--阿里巴巴 fast json依赖--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency><!--MySQL数据库驱动包的依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!---MyBatis-Plus的依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId></dependency></dependencies>

解题思路:

1.查询创建时间或者修改时间在一小时数据放到List集合中

2.循环集合数据并将每条数据对象转为JSON对象 因为ES存储的数据都是JSON数据

3.在方法上添加定时期定时器定时更新ES数据

有了思路,直接上代码

package com.tm.service;import com.alibaba.fastjson.JSON;
import com.tm.mapper.EsSyncGoodsDataMapper;
import com.tm.model.entity.EsSyncGoodsEntity;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.List;/*** @author likk* @create 2021-11-23 17:11*/
@Component
@EnableScheduling
public class EsSyncGoodsDataService {//通过ES提供的 构造器 来建立起和ES之间的远程连接private static RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.22.131", 19200, "http"));//创建高层对象准备操作ES创建的连接private static RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);@ResourceEsSyncGoodsDataMapper esSyncGoodsDataMapper;@Scheduled(cron = "* * 1 * * ?")//或直接指定时间间隔,这里是1小时public void queryEsSyncGoodsData(){//查询修改或创建的时间在一小时内的数据添加到ES中List<EsSyncGoodsEntity> list= esSyncGoodsDataMapper.queryEsSyncGoodsData();//循环 新增list.forEach(a->{try {//创建批量请求BulkRequest bulkRequest = new BulkRequest();//创建索引:IndexRequest indexRequest = new IndexRequest("goods_spu");//放入数据json字符串 类型 jsonindexRequest.source(JSON.toJSONString(a), XContentType.JSON);//esIdindexRequest.id(a.getSpuId().toString());//新增索引bulkRequest.add(indexRequest);//将数据通过bulk操作进入esrestHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println("新增成功");}catch (Exception e){e.printStackTrace();}});System.out.println(list);}}

java中SpringBoot项目定时将MySql数据同步到ES中相关推荐

  1. Elasticsearch和Hive整合,将hive数据同步到ES中

    1 Elasticsearch整合Hive 1.1 软件环境 Hadoop软件环境 Hive软件环境 ES软件环境 1.2 ES-Hadoop介绍 1.2.1 官网 https://www.elast ...

  2. mysql数据同步到es

    线上环境使用了logstash做mysql和es的数据同步.数据量过大时.可能会出现同步延时的问题. 一般同步方案有三种: 1:logstash等工具同步 2:数据库ES双写 3:消息机制 第一种有点 ...

  3. MySQL数据同步到ES集群(MySQL数据库与ElasticSearch全文检索的同步)

    简介:MySQL数据库与ElasticSearch全文检索的同步,通过binlog的设置对MySQL数据库操作的日志进行记录,利用Python模块对日志进行操作,再利用kafka的生产者消费者模式进行 ...

  4. 如何将mysql数据同步到es

    根据业务场景我们有三种方式 如果是历史数据同步我们可以用logstash同步数据库数据到es具体操作如下 1 上传并解压logstash 2 创建文件夹sync并在里面创建文件logstash-db- ...

  5. es 全量同步mysql_使用canal将mysql同步到es中

    因为自己项目中需要用到mysql数据同步到es中,查找了相关资料最后决定用canal来做,所以便有了本文,下面一起来看如何使用canal吧 canal教程 根据 https://github.com/ ...

  6. Mysql 数据同步到 Elasticsearch

    阅读目录 说明 1 同步原理 2 插件 logstash-input-jdbc go-mysql-elasticsearch elasticsearch-jdbc 3 logstash-input-j ...

  7. MYSQL数据同步到ES7

    ** 概述 ** 现在的项目数据量越来越大,全文检索功能使用场景也越来越普遍. 而我们一般的生产数据是在mysql,或其它一些数据库, 我们的产品数据就是mysql,而又要使用全文检索, 所以要把my ...

  8. es与mysql数据同步 (go-mysql-es)

    es与mysql数据同步 (go-mysql-es) 简介 go-mysql-elasticsearch是一款开源的高性能的Mysql数据同步ES的工具,其由go语言开发,编译及使用非常 简单.go- ...

  9. 物流快递系统前、后端+Java语言+SpringBoot项目+MVC三层架构+maven+Mysql+Tomcat+可以用于学习SpringBoot项目入门

    物流快递系统前.后端+Java语言+SpringBoot项目+MVC三层架构+Mysql+Tomcat+可以用于学习SpringBoot项目入门 可以用于课程设计.毕业设计的知识点入门学习 提示:此资 ...

最新文章

  1. AIX5.3安装bash shell
  2. 使用python操作redis及简单应用
  3. 4-8 string
  4. 066:ORM查询条件详解-startswith和endswith:
  5. 字符指针与字符串变量的转换
  6. 计算机专业课程项目教学教学设计,高职旅游管理专业计算机课程项目化教学设计...
  7. vba传值调用_vba – 动态调用从形状OnAction属性传递参数的宏
  8. Intent常用使用汇总
  9. XML命名空间和相关类简介
  10. 17.异常(三)之 e.printStackTrace()介绍
  11. bugzilla发送邮件慢的问题
  12. 004C语言 实现小世界网络
  13. 关于《淘宝技术这十年》
  14. Markdown入门教程-不古出品
  15. 免费动态域名解析软件nat123技术性原理分析及使用方法说明
  16. 基于matlab的动态心形图案
  17. 7-8月博乐推荐文章
  18. 读取文件云服务器bcc,读取文件云服务器bcc
  19. 微信小程序支付 tp5
  20. 基于多阈值的形态提取遥感图像中的沿海线的特征方法(Qu Jishuang)

热门文章

  1. 探索特殊符号电脑上输出
  2. Win7 屏保时 硬盘灯 常亮 频繁读写 解决方案
  3. oracle rowid java_【转载】oracle之rowid详解
  4. PC网站QQ第三方登陆
  5. 如何享受免费网络iPhone 5连接电脑上网
  6. 关闭双重验证(OTP)适用群晖7.X
  7. php undefined symbol: zip_libzip_version报错
  8. uniapp+nginx+rtmp开发直播app(1)
  9. NetCore3.1连接Redis做秒杀案例
  10. 关于android6.0打开相机自动对焦声音后,离开相机界面,还会存在声音一直响的问题