废话少说,直接上干货
pom文件的依赖
<properties>
<elasticsearch.version>7.13.0</elasticsearch.version>
</properties>
<下面是pom依赖>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.13.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
我这边用的是多模块SpringBoot 这个是service的es依赖
配好依赖完之后我们开始业务层的代码了。
思路
先到Mysql中查询到我们想要的数据
然后将查询到的数据转换成json数据然后es就可以进行存储了
代码如下
配置es的链接、
package com.tm.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author q请问请问q
* @createTime 2021年11月25日 18:20:00
*/
@Configuration
public class config {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
/**
* @title 无账号密码登录
* @updateTime 2021/11/22 20:53
*/
@Bean
public static RestHighLevelClient esRestClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
//集群配置法
new HttpHost("192.168.206.133",19200,"http")));
return client;
}
}
具体实现
package com.tm.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.tm.config.config;
import com.tm.mapper.EsSyncGoodsSpuMapper;
import com.tm.model.entity.EsSyncGoodsSpuEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.context.annotation.Configuration;
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 q请问请问q
* @createTime 2021年11月25日 18:20:00
*/
//EnableScheduling开启定时器
//Component让spring能够扫描到当前类
@EnableScheduling
@Component
public class EsSyncGoodsSpuServiceImpl {
//注入一下
@Resource
private EsSyncGoodsSpuMapper esSyncGoodsSpuMapper;
//这个定时器注解就是每过去5秒执行一下这个aaa的方法
@Scheduled(cron = "0/5 * * * * ?")
public void aaa() {
//这边是查询mysql的数据
List<EsSyncGoodsSpuEntity> list = esSyncGoodsSpuMapper.aaa();
//调用高层对象
RestHighLevelClient restHighLevelClient = config.esRestClient();
//然后我这边使用forEach循环将数据添加到es中
list.forEach(a -> {
//创建一个索引请求(这里面写的是我们想要添加的索引)
IndexRequest index = new IndexRequest("goods_spu");
//这边是获取到我们查询得到的数据将这个查询的id当成我们es中_id(不要es自带的)
index.id(a.getSpuId().toString());
//创建批量操作对象
BulkRequest request = new BulkRequest();
//这里我将查询到的数据循环转换成json
index.source(JSONObject.toJSONString(a), XContentType.JSON);
//将转换成json的数据添加到我们创建的对象中去
request.add(index);
try {
//将数据通过bulk操作进入es。。
restHighLevelClient.bulk(request, config.COMMON_OPTIONS);
} catch (Exception e) {
e.printStackTrace();
}
//打印......
System.out.println(list);
});
}
}