开发环境:InteliJ IDEA
操作系统 :macOS Mojave
Elasticsearch 版本:阿里云 5.5.3_with_X-Pack
客户端版本:REST Client 5.5.3
1. 预先创建好阿里云 ES 实例,开启公网地址访问白名单。
2. 预先创建好 index 和 mapping(使用 Kibana Dev Tools 创建)
PUT index_test
{
"mappings": {
"book": {
"properties" : {
"book_id" : {
"type":"keyword"
},
"name" : {
"type":"text"
}
}
}
}
}
3. 创建项目及 RestClient 类
4. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.gary.es</groupId>
<artifactId>rest-client-5</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/rest -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.5.3</version>
</dependency>
</dependencies>
</project>
此处的 Java REST Client Demo主要适用于阿里云ES 5.5.3版本,不兼容阿里云ES 6.3.2版本。
由于阿里云Elasticsearch实例使用5.5.3版本,所以需要您的JDK至少在 1.8
及以上版本。
5. 构建 REST Client 对象进行访问
步骤一:
由于阿里云 ES 强制要求 elasticsearch-http-basic 认证,ES 官方给出的指导是:通过 builder 构造 RestClient 对象的同时,设定 builder 的回调接口 HttpClientConfigCallback。该回调接口仅有一个实现方法 customizeHttpClient(),参数接收一个 HttpAsyncClientBuilder 对象,设置该对象的验证信息(通过 CredentialProvider),然后返回该对象。
以此方法,来给 RestClient 设置验证信息,在后续向 ES 服务端请求时,带上验证信息。
步骤二:
通过 HttpEntity,拼接 JSON 请求,通过 restClient.performRequest() 发起请求。此例主要演示:创建一条索引文档并检索该文档。
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.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import java.io.IOException;
import java.util.Collections;
public class RestClientTest {
public static void main(String[] args) {
// 步骤一:创建 RestClient 对象
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("ES实例用户名", "ES实例密码"));
RestClient restClient = RestClient.builder(new HttpHost("ES实例公网地址", 9200))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
// 步骤二:发起请求
try {
//index a document 往ES索引增加一条数据
HttpEntity entity = new NStringEntity("{\n\"book_id\":\"0001\",\n\"name\":\"Alice in Wonderland\"\n}",
ContentType.APPLICATION_JSON);
Response indexResponse = restClient.performRequest(
"PUT",
"/index_test/book/0001",
Collections.<String, String>emptyMap(),
entity);
//search a document 检索ES数据
Response response = restClient.performRequest("GET", "/index_test/book/0001",
Collections.singletonMap("pretty", "true"));
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
该文档成功创建并检索得到,请求成功!