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.
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>
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.