Configure the Java Transport clientedit

You can connect to an X-Pack or Shield protected cluster using the transport client. To learn more, see Java client and security and Using Elasticsearch Java Clients with Shield.

The only addition you need when using Elastic Cloud Enterprise is to add a header indicating which cluster to route the connections to.

Here is an example of how to create a transport client to connect to Elastic Cloud Enterprise:

// Build the settings for our client.
String clusterId = "ac01aa2425e4a5bafdebf5100af3e9b37401055b"; // Your cluster ID here
String region = "us-east-1"; // Your region here
boolean enableSsl = true;

Settings settings = Settings.settingsBuilder()
    .put("transport.ping_schedule", "5s")
    //.put("transport.sniff", false) // Disabled by default and *must* be disabled.
    .put("", clusterId)
    .put("action.bulk.compress", false)
    .put("shield.transport.ssl", enableSsl)
    .put("request.headers.X-Found-Cluster", clusterId)
    .put("shield.user", "username:password") // your shield username and password

String hostname = clusterId + "." + region + "";
// Instantiate a TransportClient and add the cluster to the list of addresses to connect to.
// Only port 9343 (SSL-encrypted) is currently supported.
Client client = TransportClient.builder()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostname), 9343));

The example is for an ES 2.1 cluster. See GitHub for examples that work with other versions.

You need to add the Shield JAR to your classpath. Examples of how to do this with various build systems are available in the Shield documentation.