Development environment: InteliJ IDEA
Operating system: Mac OS Mojave
Elastic search version: Alibaba cloud 5.5.3 with x-pack
Client version: REST Client 5.5.3
1. Create Alibaba cloud ES instance in advance and open the public address access white list.
2. Pre create index and mapping (using Kibana Dev Tools)
PUT index_test { "mappings": { "book": { "properties" : { "book_id" : { "type":"keyword" }, "name" : { "type":"text" } } } } }
3. Create project and RestClient class
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>
The Java REST Client Demo here is mainly applicable to Alibaba cloud es version 5.5.3, which is not compatible with Alibaba cloud es version 6.3.2.
Because the Alibaba cloud elastic search instance uses version 5.5.3, you need to have at least version 1.8 of your JDK.
The Java REST Client version needs to be consistent with the ES instance version.5. Build REST Client object for access
Step one:
Because Alibaba cloud es requires the elasticsearch HTTP basic authentication, the official guidance of ES is: when the RestClient object is constructed through builder, the callback interface HttpClientConfigCallback of builder is set. The callback interface has only one implementation method, customizeHttpClient(), which takes a httapasyncclientbuilder object, sets the validation information of the object (through CredentialProvider), and returns the object.
This method is used to set the authentication information for RestClient, which will be brought with the subsequent request to ES server.
Step two:
The JSON request is spliced through HttpEntity, and the request is initiated through restClient.performRequest(). The main demonstration of this example is to create an index document and retrieve it.
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) { // Step 1: create a RestClient object final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("ES Instance user name", "ES Instance password")); RestClient restClient = RestClient.builder(new HttpHost("ES Instance public network address", 9200)) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).build(); // Step 2: initiate the request try { //index a document add a piece of data to ES index 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 to retrieve ES data 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(); } } }
The document was successfully created and retrieved. The request was successful!