java中SpringBoot项目定时将MySql数据同步到ES中

项目所用依赖

<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);

    @Resource
    EsSyncGoodsDataMapper 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字符串 类型 json
                indexRequest.source(JSON.toJSONString(a), XContentType.JSON);
                //esId
                indexRequest.id(a.getSpuId().toString());
                //新增索引
                bulkRequest.add(indexRequest);
                //将数据通过bulk操作进入es
                restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
                System.out.println("新增成功");
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        System.out.println(list);
    }


}

上一篇:WPF开发随笔收录-DrawingVisual绘制高性能曲线图


下一篇:es DSL语法