概述
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
二、创建配置文件中自定义内容
elasticsearch:
urls: localhost:9200
username: elastic
password: changeme
三、配置连接池
package com.example.zhang.bootes.config;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
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 org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.util.StringUtils;
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.username}")
private String USERNAME;
@Value("${elasticsearch.password}")
private String PASSWORD;
@Value("${elasticsearch.urls}")
private String URLS;// 都好分割
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
if (StringUtils.isEmpty(URLS)) {
throw new RuntimeException("配置有问题,elasticsearch.urls为空");
}
String[] urls = URLS.split(",");
HttpHost[] httpHostArr = new HttpHost[urls.length];
for (int i=0; i<urls.length; i++) {
String urlStr = urls[i];
if(StringUtils.isEmpty(urlStr)) {
continue;
}
httpHostArr[i] = HttpHost.create(urlStr);
}
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD)); //es账号密码(默认用户名为elastic)
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(httpHostArr)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}));
return client;
}
}
四、创建实体类
package com.example.zhang.bootes.config;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
/**
* @author 风行烈
* @version 1.0
* @date 2020/11/2 14:23
*/
@Data
// 索引名称
@Document(indexName = "systems_log")
public class Systems {
@Id
private Integer id;
private String name;
private Date createTime;
}
四、创建数据连接层
package com.example.zhang.bootes.mapper;
import com.example.zhang.bootes.config.Systems;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author 风行烈
* @version 1.0
* @date 2020/11/6 16:58
*/
public interface SystemMapper extends ElasticsearchRepository<Systems,String> {
}
五、插入数据并且测试
@Autowired
private SystemMapper systemMapper;
/**
* 在es中添加数据
*/
/**
* 测试添加文档
*
* @throws IOException
*/
@Test
public void createDocument() throws IOException {
Systems systems = new Systems();
systems.setId(3);
systems.setName("张三再次操作了数据");
systems.setCreateTime(new Date());
systemMapper.save(systems);
}
最后
以上就是任性老鼠为你收集整理的SpringBoot连接Elasticsearch并插入数据的全部内容,希望文章能够帮你解决SpringBoot连接Elasticsearch并插入数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复