Transport Client is deprecated in open source Elasticsearch. For new projects, use the Java Low Level REST Client, which provides version compatibility across all Elasticsearch releases.
This topic describes how to use Transport Client 5.3.3 to connect to an Alibaba Cloud Elasticsearch V5.x cluster over TCP.
Prerequisites
Before you begin, make sure that you have:
-
JDK 1.8 or later installed. See Install a JDK
-
An Alibaba Cloud Elasticsearch V5.5.3 cluster. See Create an Alibaba Cloud Elasticsearch cluster
-
Auto Indexing enabled for the cluster. See Configure the YML file
If Auto Indexing is not enabled, an error is reported when you index a document. 
Configure network access
Choose your connection method based on where your Java client runs.
Internet (public endpoint)
If your Java client is outside a virtual private cloud (VPC):
-
Enable Public Network Access for your cluster.
-
Add your client's public IP address to the cluster's public IP address whitelist. See Configure a public or private IP address whitelist for an Elasticsearch cluster.
-
If your client is in a home network or office LAN, add the Internet egress IP address to the whitelist, not the client's private IP address.
-
Adding
0.0.0.0/0to the whitelist allows all IPv4 addresses to access the cluster. Evaluate the security risks before using this setting. -
If the whitelist is missing or incorrect, the connection fails with a
Timeout connectingerror. -
To access the Kibana node from a client, configure a separate Kibana whitelist. See Configure a public or private IP address whitelist for Kibana.
Same VPC (internal endpoint)
If your Java client runs in the same VPC as the Elasticsearch cluster, use the cluster's internal endpoint. The private IP whitelist defaults to 0.0.0.0/0.
Add POM dependencies
Add the following dependencies to your pom.xml:
<repositories>
<!-- Elasticsearch Maven repository -->
<repository>
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
A remote code execution (RCE) vulnerability may exist in Apache Log4j. See Vulnerability notice | RCE vulnerability in Apache Log4j 2 for details and mitigation steps.
Connect to the cluster
The following code shows how to initialize a Transport Client connection. Replace the placeholders with your actual cluster values.
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 java.net.InetAddress;
// Initialize the Transport Client
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", "<cluster-id>") // Your cluster ID
.put("xpack.security.user", "elastic:<password>") // Username and password
.put("client.transport.sniff", false) // Must be false
.build())
.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("<endpoint>"), 9300)); // Endpoint (no http://) and port
// Close the client when done
client.close();
| Parameter | Description |
|---|---|
cluster.name |
The ID of your Elasticsearch cluster. Get it from the Basic Information page. See View the basic information of a cluster. |
xpack.security.user |
The username and password in the format username:password. The default username is elastic. To reset the password, see Reset the access password for an Elasticsearch cluster. |
client.transport.sniff |
Set to false. |
InetAddress.getByName() |
The cluster endpoint and port 9300. The endpoint must not start with http://. |
Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<cluster-id> |
Your Elasticsearch cluster ID | es-cn-n6w1rux8i000w**** |
<password> |
The password for the elastic user |
— |
<endpoint> |
The cluster's public or internal endpoint | es-cn-n6w1rux8i000w****.public.elasticsearch.aliyuncs.com |
Example
The following example connects to the cluster, writes a document to an index, and retrieves it. Download the complete sample code.
import org.elasticsearch.action.get.GetResponse;
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 {
// Initialize the client
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", "es-cn-n6w1rux8i000w****")
.put("xpack.security.user", "elastic:es_password")
.put("client.transport.sniff", false)
.build())
.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("es-cn-n6w1rux8i000w****.public.elasticsearch.aliyuncs.com"), 9300));
// Write a document to the index
IndexResponse idxResp3 = client.prepareIndex("test_index", "test_type", "333")
.setSource(jsonBuilder()
.startObject()
.field("user_id", "333")
.field("email", "a***@aliyun.com")
.endObject()
)
.get();
System.out.println(idxResp3.toString());
// Retrieve the document
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);
}
}
}
Troubleshooting
`NoNodeAvailableException`
This error occurs when you use Transport Client 5.5 or 5.6 to connect to an Elasticsearch V5.x cluster. Use Transport Client 5.3.3 instead.
`Timeout connecting`
The cluster cannot be reached. Check that:
-
Your client's IP address is in the cluster's whitelist (public or private, depending on your network)
-
You are using the correct endpoint (public vs. internal) for your network environment
-
The whitelist entry uses the Internet egress IP, not the private IP, for home or office LAN clients
Auto Indexing error on document write
If an error appears when writing a document, Auto Indexing may be disabled. Enable it in the cluster's YML configuration. See Configure the YML file.
What's next
-
Java Low Level REST Client — the recommended client for new projects, with full version compatibility