Accessing an Elasticsearch Cluster Through the Transport Client
You can use Transport Client to access a CSS cluster with the security mode disabled. For a cluster in security mode, you are advised to use Accessing an Elasticsearch Cluster Through the High Level REST Client.
Precautions
- You are advised to use a Transport Client version that matches the Elasticsearch version. For example, use Transport Client 7.6.2 to access an Elasticsearch 7.6.2 cluster.
- This solution is suitable for clusters that are not using the security mode. Such clusters can only be accessed through private IP addresses.
- Transport Client has been deprecated in Elasticsearch 7.x. You are advised to use High Level Rest Client to access clusters instead.
Prerequisites
- The CSS cluster is available.
- Ensure that the server running Java can communicate with the CSS cluster.
- Install JDK 1.8 on the server. You can download JDK 1.8 from: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- Declare Java dependencies.
7.6.2 indicates the version of the Elasticsearch Java client.
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>7.6.2</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.6.2</version> </dependency>
Accessing a Cluster
The following code is an example of using Transport Client to connect to the Elasticsearch cluster and check whether the test index exists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.concurrent.ExecutionException; public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException, UnknownHostException { String cluster_name = "xxx"; String host1 = "xx.xx.xx.xx"; String host2 = "y.y.y.y"; Settings settings = Settings.builder() .put("client.transport.sniff",false) .put("cluster.name", cluster_name) .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName(host1), 9300)) .addTransportAddress(new TransportAddress(InetAddress.getByName(host2), 9300)); IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest("test"); ActionFuture<IndicesExistsResponse> exists = client.admin().indices().exists(indicesExistsRequest); System.out.println(exists.get().isExists()); } } |
In the information above, cluster_name indicates the cluster name, and host1 and host2 indicate the IP addresses of the cluster nodes. You can run the GET _cat/nodes command to obtain the IP addresses of the nodes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot