Java ElasticSearch REST客户端
Posted 王疏蔬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java ElasticSearch REST客户端相关的知识,希望对你有一定的参考价值。
一、基础信息
1、pom.xml(根据业务选择适合版本)
<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>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>$elasticsearch.version</version>
</dependency>
2、nacos配置(根据业务选择适合模式)
elasticsearch:
prefix: http
uris: es-cn-**********************.aliyun.com:9200
username: esadmin
password: 123456
3、创建ElasticSearchConfig(项目启动自动注入)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig
//http
@Value("$elasticsearch.prefix")
private String prefix;
//es连接url(多个url用","隔开)
@Value("$elasticsearch.uris")
private String esUrl;
//账户(没有可不填)
@Value("$elasticsearch.username")
private String username;
//密码(没有可不填)
@Value("$elasticsearch.password")
private String password;
//方法
二、低级客户端创建
@Bean
public RestClient restClient()
//处理多连接模式
String[] split = esUrl.split(",");
HttpHost[] httpHost = new HttpHost[split.length];
for (int i = 0; i < split.length; i++)
String[] item = split[i].split(":");
httpHost[i] = new HttpHost(item[0], Integer.parseInt(item[1]), prefix);
return RestClient.builder(httpHost).build();
三、高级客户端创建
@Bean
public RestHighLevelClient restHighLevelClient()
//处理多连接模式
String[] split = esUrl.split(",");
HttpHost[] httpHost = new HttpHost[split.length];
for (int i = 0; i < split.length; i++)
String[] item = split[i].split(":");
httpHost[i] = new HttpHost(item[0], Integer.parseInt(item[1]), prefix);
return new RestHighLevelClient(RestClient.builder(httpHost));
四、高级客户端创建(带用户名和密码)
@Bean
public RestHighLevelClient restHighLevelClient()
//处理多连接模式
String[] split = esUrl.split(",");
HttpHost[] httpHost = new HttpHost[split.length];
for (int i = 0; i < split.length; i++)
String[] item = split[i].split(":");
httpHost[i] = new HttpHost(item[0], Integer.parseInt(item[1]), prefix);
//需要用户名和密码的认证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder restClientBuilder = RestClient.builder(httpHost)
.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
return new RestHighLevelClient(restClientBuilder);
附:增加部分参数
RestClientBuilder restClientBuilder = RestClient.builder(httpHost)
// 认证和线程数
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
int threadCount = 10;
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build());
return httpClientBuilder;
)
// 超时超时设置
.setRequestConfigCallback(requestConfigCallback ->
requestConfigCallback.setConnectTimeout(10);
requestConfigCallback.setSocketTimeout(10);
return requestConfigCallback;
);
以上是关于Java ElasticSearch REST客户端的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介Java REST ClientJava ClientSpri(代码
使用Java High Level REST Client操作elasticsearch
Elasticsearch Java高级REST客户端建立一堆TCP连接,并且在放入数据后不要关闭该连接