springboot整合es 操作数据库导入到es

1.引入相关依赖

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>
        //提供getset 让代码更加简洁
       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        //mybatis 提供分页
          <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.3.0</version>
        </dependency>

2.yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/nsbdcms?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
  data:
    elasticsearch:
      cluster-name: es-Cluster
      cluster-nodes: 192.168.126.128:9200
      repositories:
        enabled: true
es:
  url: 192.168.126.128:9200
  index: sougoulog

#mybatis plus 设置
mybatis-plus:
  type-aliases-package: com.example.jsoupdome.domin
  mapper-locations: classpath:mapper/*.xml
  configuration:
    jdbc-type-for-null: null
  global-config:
    # 关闭 mybatis-plus的 banner
    banner: false

2.1创建配置类 Client

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhang
 * @version 1.0
 * @date 2021/10/27 15:11
 */
@Configuration
public class Client {
        @Value("${es.url}")
        private String esUrl;

        @Bean
        RestHighLevelClient configRestHighLevelClient() throws Exception {

        String[] esUrlArr = esUrl.split(",");

        List<HttpHost> httpHosts = new ArrayList<>();
        for(String es : esUrlArr){
            String[] esUrlPort = es.split(":");
            httpHosts.add(new HttpHost(esUrlPort[0], Integer.parseInt(esUrlPort[1]), "http"));
        }
        return new RestHighLevelClient(
                RestClient.builder(httpHosts.toArray(new HttpHost[0]))
        );
    }

3.创建实体类对应于数据库表 一一对应列表信息

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

import java.util.Date;

@Data
@TableName(value = "t_cms_content_news")
@Document(indexName = "content")
public class TCmsContentNews {
    @TableId(value = "content_id",type = IdType.INPUT)
    private Long contentId;
    @ApiModelProperty(value="内容")
    private String content;
    @ApiModelProperty(value="附件")

    private String  fujian;
    @ApiModelProperty(value="来源")
    private String laiyuan;
    private String val;
    @TableField("create_time")
    private Date createTime;
    @TableField("update_time")
    private Date updateTime;
}

4.ContentEsRepository接口 继承 ElasticsearchRepository 这个类,提供操作es api 直接用就可以
import com.example.jsoupdome.domin.TCmsContentNews;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;


/**
 * @author zhang
 * @version 1.0
 * @date 2021/10/28 16:18
 */
public interface ContentEsRepository extends ElasticsearchRepository<TCmsContentNews,Long> {


}

5.然后就三层架构了

controller  


import com.example.jsoupdome.service.ContentEsServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 向es导入内容表
 * @author zhang
 * @version 1.0
 * @date 2021/10/28 14:50
 */
@RestController
@RequestMapping("/update")
public class ContentEsController {

    @Autowired
    private ContentEsServce contentEsServce;
    /**
     * 创建索引并存入内容
     * */
    @RequestMapping(value = "/importEs",method = RequestMethod.POST)
    public String importEsByContent(){
        this.contentEsServce.saveEsByContent();
        return "success" ;
    }

}
ContentEsServce接口
/**
 * @author zhang
 * @version 1.0
 * @date 2021/10/28 15:52
 */
public interface ContentEsServce {
    void saveEsByContent();
}
ContentEsServceImpl实现类

import com.example.jsoupdome.dao.ContentMapper;
import com.example.jsoupdome.repository.ContentEsRepository;
import com.example.jsoupdome.domin.TCmsContentNews;
import com.example.jsoupdome.service.ContentEsServce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhang
 * @version 1.0
 * @date 2021/10/28 15:53
 */
@Service
public class ContentEsServceImpl implements ContentEsServce {
    @Autowired
    private ContentEsRepository contentEsRepository;
    @Autowired
    private ContentMapper contentMapper;
    public static ThreadPoolExecutor es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.SECONDS,new LinkedBlockingQueue());
    @Override
    public void saveEsByContent() {
        //查询全部数据
        List<TCmsContentNews> content = this.contentMapper.selectList(null);
        //获取总数
        int total = content.size();
        //定义每页个数 --前台传来
        final int pagesize = 100;
        //求出当每页为pagesize 时候,可以分几页,就循环几次 ,分页插入
        int pagenum = (int) Math.ceil(total / pagesize);
        System.out.println("一共有"+pagenum+"页");
        //分页导入
        for (int i = 1; i <= pagenum; i++) {
            //分页对象 mybatis 提供的
            PageHelper.startPage(i,pagesize);
            PageInfo<TCmsContentNews> tcmFilePageInfo = new PageInfo(content);
            System.out.println("正在导入第" + i + "页数据");
        if(tcmFilePageInfo.getSize()>0){
            es.submit(()->{
                contentEsRepository.saveAll(tcmFilePageInfo.getList());
            });
        }
        }
        System.out.println("导入完毕!");
    }
}

dao层

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.jsoupdome.domin.TCmsContentNews;

/**
 * @author zhang
 * @version 1.0
 * @date 2021/10/28 17:01
 */
public interface ContentMapper extends BaseMapper<TCmsContentNews> {
}

然后 post 请求一下 http://localhost:8888/update/importEs

我们打开head 插件查询数据

说明插入成功 了 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值