This topic describes how to use Transport Client 5.3.3 to access a cluster of Alibaba Cloud Elasticsearch V5.X.

Precautions

We recommend that you use Transport Client 5.3.3 to access an Elasticsearch cluster. If you use Transport Client 5.5 or 5.6 to assess an Elasticsearch cluster, the error message "NoNodeAvailableException" is displayed.

Java Transport Client communicates with Elaticsearch over TCP. If the client communicates with different versions of Elasticsearch, incompatibilities may occur. In the later versions of open-source Elasticsearch, Transport Client is deprecated. We recommended that you use Java Low Level REST Client to access the Elasticsearch cluster to ensure compatibility.

This topic only provides sample code. For more information, see Call the document API.

Preparations

  • Install a JDK. The JDK version must be 1.8 or later.

    For more information, see Install the JDK.

  • Create a cluster of Alibaba Cloud Elasticsearch V5.5.

    For more information, see Create an Elasticsearch cluster.

  • Enable the Auto Indexing feature for the Elasticsearch cluster.

    For more information, see Enable auto indexing.

    If the Auto Indexing feature is not enabled, the following error message is displayed.Error
  • Enable the public endpoint of your Elasticsearch cluster and add the public IP address of your host to the public whitelist.

    For more information, see Configure a whitelist to access an Elasticsearch cluster over the Internet or a VPC.

    Note If you want to query the public IP address of your host, open Google Chrome, enter IP, and click What Is My IP Address. You can obtain the public IP address of your host on the web page that appears.
  • Create a Java Maven project and add the Project Object Model (POM) dependencies to the pom.xml file of the Java project. For more information, see POM dependencies.

POM dependencies

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>loghub-storm-spout</artifactId>
        <version>5.3.3</version>
     </dependency>
     <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch-hadoop-mr</artifactId>
        <version>5.3.3</version>
     </dependency>
     <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
     </dependency>
     <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
     </dependency>
</dependencies>

Example

package me.gary.test.es;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

import static org.elasticsearch.common.xcontent.XContentFactory.*;

import java.net.InetAddress;

public class TransportClientDemo {

    public static void main(String[] args) {
        try {
            TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
                    .put("cluster.name", "es-cn-mpddddddt5l") //Set the value to the ID of the target Elasticsearch cluster.
                    .put("xpack.security.user", "elastic:123456") //Set the value to the username and password for accessing the target Elasticsearch cluster.
                    . put("client.transport.sniff", false) //set the value to false.
                    .build())
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("es-cn-mpddddddt5l.public.elasticsearch.aliyuncs.com"), 9300));
                     //Specify the endpoint and port.
            //You can modify the following code as required, for example, you can change the values of the index, type, and document ID.
            IndexResponse idxResp3 = client.prepareIndex("test_index", "test_type", "333")
                     setSource(jsonBuilder()
                            .startObject()
                            .field("user_id", "333")
                            .field("email", "test@aliyun.com")
                            .endObject()
                    )
                    .get();

            System.out.println(edge.toString());


            GetResponse getResp =
                    client.prepareGet().setIndex("test_index").setType("test_type").setId("333").execute().get();
            System.out.println(getResp.getSourceAsString());
            client.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Parameter Description
cluster.name Set the value to the ID of the Elasticsearch cluster.
client.transport.sniff Set the value to false.
xpack.security.user Set the value to the username and password for accessing the Elasticsearch cluster.
InetAddress.getByName() Specify the endpoint and port number of the Elasticsearch cluster that you want to access.
Notice The endpoint cannot start with http.