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依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>4.4.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
示例
以下代码为使用Java Low Level REST ClientI对Elasticsearch进行了查询,可供参考。
import java.io.IOException;
import org.apache.http.HttpEntity;
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.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
public class RestClientTest742 {
public static void main(String[] args) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//访问用户名和密码为您创建Elasticsearch实例时设置的用户名和密码。
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{访问用户名}", "{访问密码}"));
// 通过builder创建rest client,配置http client的HttpClientConfigCallback。
// ES HTTP URL 在Baidu Elasticsearch界面中可以查询
RestClient restClient = RestClient.builder(new HttpHost("{ES HTTP URL}", 8200))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
try {
// 创建request
Request request = new Request(
"POST",
"/{index_name}/_search");
// 配置请求体
request.setEntity(new NStringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON));
Response response = restClient.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上示例代码中带{}
的参数需要替换为您具体业务的参数,详情请参见代码注释。
更多Java Low Level REST Client的使用特性,请参见Java Low Level REST Client官方文档。