×
Community Blog Mastering Elasticsearch with Alibaba Cloud: A Guide to Using Java API Client (8.x)

Mastering Elasticsearch with Alibaba Cloud: A Guide to Using Java API Client (8.x)

This guide covers setup, essential configurations, and includes sample code to help you get started quickly.

Introduction

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.

Background Information

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.

Components of the Java API Client

  • ElasticsearchClient class: Core class providing methods to communicate with the Elasticsearch server.
  • JSON ObjectMapper: Facilitates serialization and deserialization using the Jackson library.
  • Common Capabilities: Includes features like connection pooling, retry mechanisms, and JSON serialization, enhancing code readability and maintainability.

Preparations

Step 1: Install JDK

Ensure you have a JDK version 1.8 or later installed. For installation instructions, see Install a JDK.

Step 2: Create an Alibaba Cloud Elasticsearch Cluster

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.

Step 3: Enable Auto Indexing

Enable the Auto Indexing feature for seamless operations. For more information, see Configure the YML file.

Step 4: Configure IP Address Whitelist

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.

Establishing Communication

Initialize the Java Low Level REST Client

// Create the low-level client
RestClient restClient = RestClient.builder(
    new HttpHost("localhost", 9200, "http")).build();

Create the Transport Client with JacksonJsonpMapper

// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
    restClient, new JacksonJsonpMapper());

Instantiate the ElasticsearchClient

// Create the API client
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
System.out.println("elasticsearchClient = " + elasticsearchClient);

Define POM Dependencies

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>

Example: Managing an Index

Sample Code to Create and Manage Index

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();
        }
    }
}

High-Concurrency Scenario

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();

Conclusion

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

0 1 0
Share on

Data Geek

97 posts | 4 followers

You may also like

Comments

Data Geek

97 posts | 4 followers

Related Products

  • Alibaba Cloud Elasticsearch

    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 More
  • OpenAPI Explorer

    OpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.

    Learn More
  • API Gateway

    API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.

    Learn More
  • CloudBox

    Fully managed, locally deployed Alibaba Cloud infrastructure and services with consistent user experience and management APIs with Alibaba Cloud public cloud.

    Learn More