Low Level REST Client
更新时间:2020-08-18
本文基于Java Low Level REST Client 7.x版本,为您介绍Elasticsearch Java API的用法。
准备工作
- 安装Java,要求JDK版本为1.8及以上。
-
创建Baidu Elasticsearch实例,版本7.4.2。
注意 Low Level Client能够与任何版本的Elasticsearch兼容,因此客户端版本可以为任何版本,本文以7.4.2版本为例。
- 创建Java Maven工程,并将如下的pom依赖添加到Java工程的pom.xml文件中。
pom依赖
XML
1<dependency>
2 <groupId>org.elasticsearch.client</groupId>
3 <artifactId>rest</artifactId>
4 <version>7.4.2</version>
5</dependency>
6<dependency>
7 <groupId>org.apache.httpcomponents</groupId>
8 <artifactId>httpasyncclient</artifactId>
9 <version>4.1.4</version>
10</dependency>
11<dependency>
12 <groupId>org.apache.httpcomponents</groupId>
13 <artifactId>httpcore-nio</artifactId>
14 <version>4.4.13</version>
15</dependency>
16<dependency>
17 <groupId>org.apache.httpcomponents</groupId>
18 <artifactId>httpclient</artifactId>
19 <version>4.5.6</version>
20</dependency>
21<dependency>
22 <groupId>org.apache.httpcomponents</groupId>
23 <artifactId>httpcore</artifactId>
24 <version>4.4.12</version>
25</dependency>
26<dependency>
27 <groupId>commons-codec</groupId>
28 <artifactId>commons-codec</artifactId>
29 <version>1.10</version>
30</dependency>
31<dependency>
32 <groupId>commons-logging</groupId>
33 <artifactId>commons-logging</artifactId>
34 <version>1.2</version>
35</dependency>
示例
以下代码为使用Java Low Level REST ClientI对Elasticsearch进行了查询,可供参考。
Java
1import java.io.IOException;
2import org.apache.http.HttpEntity;
3import org.apache.http.HttpHost;
4import org.apache.http.auth.AuthScope;
5import org.apache.http.auth.UsernamePasswordCredentials;
6import org.apache.http.client.CredentialsProvider;
7import org.apache.http.entity.ContentType;
8import org.apache.http.impl.client.BasicCredentialsProvider;
9import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
10import org.apache.http.nio.entity.NStringEntity;
11import org.apache.http.util.EntityUtils;
12import org.elasticsearch.client.Request;
13import org.elasticsearch.client.Response;
14import org.elasticsearch.client.RestClient;
15import org.elasticsearch.client.RestClientBuilder;
16
17public class RestClientTest742 {
18 public static void main(String[] args) {
19 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
20 //访问用户名和密码为您创建Elasticsearch实例时设置的用户名和密码。
21 credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{访问用户名}", "{访问密码}"));
22 // 通过builder创建rest client,配置http client的HttpClientConfigCallback。
23 // ES HTTP URL 在Baidu Elasticsearch界面中可以查询
24 RestClient restClient = RestClient.builder(new HttpHost("{ES HTTP URL}", 8200))
25 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
26 @Override
27 public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
28 return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
29 }
30 }).build();
31 try {
32 // 创建request
33 Request request = new Request(
34 "POST",
35 "/{index_name}/_search");
36 // 配置请求体
37 request.setEntity(new NStringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON));
38 Response response = restClient.performRequest(request);
39 System.out.println(EntityUtils.toString(response.getEntity()));
40 } catch (IOException e) {
41 e.printStackTrace();
42 }
43 }
44}
以上示例代码中带{}
的参数需要替换为您具体业务的参数,详情请参见代码注释。
更多Java Low Level REST Client的使用特性,请参见Java Low Level REST Client官方文档。