Elasticsearch provides robust RESTful API operations, making it a go-to solution for data management. With the introduction of the Java API Client from version 7.17 onwards, the previous Java REST Client has been deprecated. This guide focuses on using the Java API Client (8.x) to manage your Alibaba Cloud Elasticsearch cluster.
The Java API Client is a versatile library that establishes communication between your Java application and the Elasticsearch server. It simplifies the development process and ensures maintainable code.
Ensure you have a JDK version 1.8 or later installed. For installation instructions, see Install a JDK.
Create a cluster whose version is equal to or newer than the Java API Client. For detailed steps, see Create an Alibaba Cloud Elasticsearch cluster.
Enable the Auto Indexing feature for seamless operations. For more information, see Configure the YML file.
Ensure the server running your Java code can communicate with the Elasticsearch cluster by configuring the appropriate IP address whitelist. For more details, see Configure a public or private IP address whitelist.
// Create the low-level client
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http")).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// Create the API client
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
System.out.println("elasticsearchClient = " + elasticsearchClient);
Ensure you include the following dependencies in your POM file. Replace 8.x
with your specific Java API Client version.
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.x</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
Replace the placeholders {}
with your specific parameters.
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
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.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;
public class RestClientTest {
public static void main(String[] args) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{Username}", "{Password}"));
RestClient restClient = RestClient.builder(new HttpHost("{Public endpoint of the Elasticsearch cluster}", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
try {
CreateIndexResponse indexRequest = elasticsearchClient.indices().create(createIndexBuilder -> createIndexBuilder
.index("{index_name}")
.aliases("{foo}", aliasBuilder -> aliasBuilder.isWriteIndex(true))
);
System.out.println("Index document successfully! " + indexRequest.acknowledged());
DeleteIndexResponse deleteResponse = elasticsearchClient.indices().delete(createIndexBuilder -> createIndexBuilder
.index("{index_name}")
);
System.out.println("Delete document successfully! \n" + deleteResponse.toString());
IndicesResponse indicesResponse = elasticsearchClient.cat().indices();
indicesResponse.valueBody().forEach(info -> System.out.println(info.health() + "\t" + info.status() + "\t" + info.index() + "\t" + info.uuid() + "\t" + info.pri() + "\t" + info.rep()));
transport.close();
restClient.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
Add configurations related to the number of client connections to handle high concurrency:
httpClientBuilder.setMaxConnTotal(500);
httpClientBuilder.setMaxConnPerRoute(300);
For example:
String host = "localhost";
int port = 9200;
String username = "elastic";
String password = "passwd";
final int max_conn_total = 500;
final int max_conn_per_route = 300;
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(max_conn_total);
httpClientBuilder.setMaxConnPerRoute(max_conn_per_route);
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
The Java API Client offers powerful features for managing your Alibaba Cloud Elasticsearch cluster efficiently. By following this guide, you'll be equipped to make the most out of your Elasticsearch operations.
Ready to start your journey with Elasticsearch on Alibaba Cloud? Explore our tailored Cloud solutions and services to transform your data into a visual masterpiece.
Click here to embark on Your 30-Day Free Trial
Mastering Elasticsearch on Alibaba Cloud with Curl Commands and API Operations
Elevate Your Elasticsearch Experience with Java High Level REST Client (7.x)
Data Geek - July 25, 2024
Data Geek - July 23, 2024
Data Geek - June 5, 2024
Alibaba Cloud Community - April 15, 2024
Data Geek - July 10, 2024
Data Geek - July 29, 2024
Alibaba Cloud Elasticsearch helps users easy to build AI-powered search applications seamlessly integrated with large language models, and featuring for the enterprise: robust access control, security monitoring, and automatic updates.
Learn MoreOpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.
Learn MoreAPI Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.
Learn MoreFully managed, locally deployed Alibaba Cloud infrastructure and services with consistent user experience and management APIs with Alibaba Cloud public cloud.
Learn MoreMore Posts by Data Geek