栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

项目所用依赖

4.0.0

    tm-shop-model
    


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.google.guava
            guava
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.apache.commons
            commons-lang3
        
        
            org.apache.httpcomponents
            httpclient
        
        
            commons-io
            commons-io
        
        
            org.apache.httpcomponents
            httpcore
        
        
            org.jsoup
            jsoup
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            io.springfox
            springfox-swagger2
        
        
            io.springfox
            springfox-swagger-ui
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
        
            io.jsonwebtoken
            jjwt
        
        
            org.springframework.boot
            spring-boot-starter-redis
        
        
        
            com.alibaba
            fastjson
        
        
            joda-time
            joda-time
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            com.aliyun.oss
            aliyun-sdk-oss
        
    

解题思路:

        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;


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


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/600113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号