Spring Boot整合Elasticsearch完整版之数据导入篇

1 安装Elasticsearch

windows、linux,安装都是大同小异的,本章所有的操作都是基于Docker容器来操作的

docker虚拟机Ip地址:10.211.55.4

1.1 docker镜像下载
docker pull elasticsearch:5.6.8

1.2 安装es容器

docker run -di --name=elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8

1.3 查看是否安装成功

http://10.211.55.4:9200/

9200端口(Web管理平台端口) 9300(服务默认端口)

1.4 开启远程连接

elasticsearch从5版本以后默认不开启远程连接,所以现在连接是会报错的需要修改es开启远程连接的
1.4.1 登录容器
docker exec -it elasticsearch /bin/bash
1.4.2 进入到es容器目录
cd /usr/share/elasticsearch/config
1.4.3 修改elasticsearch.yml文件 改成如下配置
http.host: 0.0.0.0
transport.host: 0.0.0.0
#discovery.zen.minimum_master_nodes: 1
#集群节点的名称 my-application
cluster.name: my-application
# 开启跨域
http.cors.enabled: true
# 允许哪些可以跨域
http.cors.allow-origin: "*"
# 网络地址
network.host: 10.211.55.4
1.4.4 重启es并设置成开机自启
docker restart elasticsearch
docker update --restart=always elasticsearch

2 安装IK分词器

备注 使用时包括kibana,es,ik分词器 版本都必须相同才能使用

2.1 下载解压ik分词器

https://github.com/medcl/elasticsearch-analysis-ik/releases

unzip elasticsearch-analysis-ik-5.6.8.zip

mv elasticsearch ik

2.2 将ik目录拷贝到docker容器的plugins目录下

docker cp ./ik changgou_elasticsearch:/usr/share/elasticsearch/plugins

2.3 ik分词器测试

访问:http://10.211.55.4:9200/_analyze?analyzer=ik_smart&pretty=true&text=我爱你中国

3 创建Spring Boot工程 如下

备注

controller 访问控制器 
dao mybatis mapper接口
entity 自己封装的返回体
es  es mapper 接口
pojo  mysql和es的实体封装
servie与impl  接口与实现层

application.yml 配置如下

server:
  port: 10086
spring:
  application:
    name: search
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/es?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
  # es索引库的连接信息
  data:
    elasticsearch:
      cluster-name: my-application
      cluster-nodes: 10.211.55.4:9300
#超时配置
ribbon:
  ReadTimeout: 300000

mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.elasticsearch.pojo

logging:
  level:
    com.elasticsearch.dao: debug
项目结构

Spring Boot整合Elasticsearch完整版之数据导入篇_第1张图片

3.1 添加spring-data-es


    org.springframework.boot
    spring-boot-starter-data-elasticsearch

3.2 添加启动类

package com.elasticsearch;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

/**
 * @Author cxk
 * @date 2020/10/7 16:53
 */
@SpringBootApplication
@MapperScan(basePackages = {"com.elasticsearch.dao"})
@EnableElasticsearchRepositories(basePackages = "com.elasticsearch.es") //一定要开启哈
public class ElasticsearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(ElasticsearchApplication.class, args);
    }
}

3.2.1 创建控制器

package com.elasticsearch.controller;

import com.elasticsearch.entity.Result;
import com.elasticsearch.entity.StatusCode;
import com.elasticsearch.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author cxk
 * @date 2020/10/7 20:17
 */
@RequestMapping("/search")
@RestController
@CrossOrigin
public class EsController {

    /**
     * 导入数据到索引库
     */
    @Autowired
    private SkuInfoService skuInfoService;
    @GetMapping("/import")
    public Result search(){
       skuInfoService.importData();
       return new Result(true, StatusCode.OK,"导入数据到索引库成功");
    }
}

3.2.2 创建service接口

package com.elasticsearch.service;

/**
 * @Author cxk
 * @date 2020/10/7 20:43
 */
public interface SkuInfoService {

    void  importData();

}

3.2.3 创建mysqlMapper与esMapper

es

package com.elasticsearch.es;

import com.elasticsearch.pojo.SkuMap;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * @Author cxk
 * @date 2020/10/7 21:07
 */
@Repository
public interface SkuEsMapper extends ElasticsearchRepository<SkuMap, Long> {
}
mysql

package com.elasticsearch.dao;


import com.elasticsearch.pojo.SkuInfo;

import java.util.List;

public interface SkuInfoMapper {

    List<SkuInfo> findAll();
}

3.2.4 创建mysql 实体与es实体

mysql实体 skuinfo
package com.elasticsearch.pojo;

import lombok.Data;

import java.util.Date;

@Data
public class SkuInfo {
    private Long id;

    private String sn;

    private String name;

    private Integer price;

    private Integer num;

    private Integer alertNum;

    private String image;

    private String images;

    private Integer weight;

    private Date createTime;

    private Date updateTime;

    private Long spuId;

    private Integer categoryId;

    private String categoryName;

    private String brandName;

    private String spec;

    private Integer saleNum;

    private Integer commentNum;

    private String status;
} 
es 实体 skuMap
package com.elasticsearch.pojo;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @Author cxk
 * @date 2020/10/7 21:04
 * skuinfo转换成索引库的实体
 */
@Data
/**
 * indexName = skumap  索引库的名称  type 可写可不写
 */
@Document(indexName = "skumap", type = "docs")
public class SkuMap implements Serializable {
    //@id 表示文档的唯一标识
    @Id
    private Long id;

    //SKU名称
    /**
     * sku 名称
     * type = FieldType.Text  text 支持分词
     * index = true  添加数据时.是否分词
     * analyzer = ik_smart  创建索引的分词器
     * store = false  是否存储
     * searchAnalyzer = "ik_smart"  搜索时使用的分词器
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart")
    private String name;

    //商品价格,单位为:元
    @Field(type = FieldType.Integer)
    private Integer price;

    //库存数量

    private Integer num;

    //商品图片
    //@Field(index = false)
    private String image;

    //商品状态,1-正常,2-下架,3-删除
    private String status;

    //创建时间
    private Date createTime;

    //更新时间
    private Date updateTime;

    //SPUID
    private Long spuId;

    //类目ID
    private Long categoryId;

    //类目名称
    // FieldType.Keyword 是一个关键字 (keyword) 表示不分词.
    /**
     * FieldType.Keyword  不分词
     */
    @Field(type = FieldType.Keyword)
    private String categoryName;

    //品牌名称
    @Field(type = FieldType.Keyword)
    private String brandName;

    //规格
    private String spec;

    //动态的域的添加和变化

    //规格参数
    //@Field(type = FieldType.Object)
    private Map<String, Object> specMap;
}

3.2.5 service实现层

package com.elasticsearch.service.impl;

import com.alibaba.fastjson.JSON;
import com.elasticsearch.es.SkuEsMapper;
import com.elasticsearch.dao.SkuInfoMapper;
import com.elasticsearch.pojo.SkuInfo;
import com.elasticsearch.pojo.SkuMap;
import com.elasticsearch.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author cxk
 * @date 2020/10/7 20:42
 */
@Service
public class SkuInfoServiceImpl implements SkuInfoService {

    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Autowired
    private SkuEsMapper skuEsMapper;
    /**
     * 导入到数据到索引库
     */
    @Override
    public void importData() {
        List<SkuInfo> list = skuInfoMapper.findAll();
        //将数据库中取到的数据转换成索引库实体 skuinfo-->skuMap
        List<SkuMap> skuMapList= JSON.parseArray(JSON.toJSONString(list),SkuMap.class);
        //如果需要生成动态域,只需要将该域存入到一个Map 中,
        // 该Map中的key值会生成一个域,域的名字为该map的key
        for (SkuMap map : skuMapList) {
            Map<String, Object> specMap = JSON.parseObject(map.getSpec(),Map.class);
            map.setSpecMap(specMap);
        }
        skuEsMapper.saveAll(skuMapList);
    }

}

4 测试

4.1 请求

http://127.0.0.1:10086/search/import

4.2 查看索引库信息

9100端口 是es-head的对外端口也可以用kibana来代替 这里就没有做说明了
http://10.211.55.4:9100/
可以看到索引库中多了skumap

Spring Boot整合Elasticsearch完整版之数据导入篇_第2张图片

skumap的数据

后期我们在索引库的操作

demo地址

https://e.coding.net/mzjmc/elasticsearch/elasticsearch.git

你可能感兴趣的:(java,编程语言,系统运维,mysql,docker,elasticsearch,java,mysql,spring,boot)