Connecting to Elasticsearch Cluster Using Java API

We can use Elasticsearch Client to connect to elasitcsearch. There are Transport Client and Node Client to connect to Elasticsearch cluster. Node is very lowever level client and knows the cluster very well. However, the TransportClient connects remotely to an Elasticsearch cluster using the transport module. It does not join the cluster, but simply gets one or more initial transport addresses and communicates with them in round robin fashion on each action. It is recommended to use Tranport client. After 5.0.0 version, we need to add following dependency for Transport client to make it work along with core jar.


<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
     <groupId>org.elasticsearch.client</groupId>
     <artifactId>transport</artifactId>
     <version>5.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
     <groupId>org.elasticsearch</groupId>
     <artifactId>elasticsearch</artifactId>
     <version>5.2.0</version>
</dependency>

We can initialize the Elasticsearch Client as following using JAVA API. It is not recomended to create new connection for every call.


/**
* Class to initialized the Elasticsearch client. It will re-use the same client
*
* @author vsubedi
*
*/
public class ElasticsearchClient{

private static Client client;

/**
* Initialized the Elasticsearch Client if the client is not null. If the
* client is not null, that client will be used
*
* @param clustername - clusername
* @param ipAddress -ES ip address
* @param port -ES port
* @return @Client
* @throws UnknownHostException
*/
@SuppressWarnings("resource")
public static Client getElasticsearchClient(String clustername, String ipAddress, int port) throws UnknownHostException{

   if(client != null){
       return client;
   }

   Settings settings = Settings.builder().put("cluster.name", clustername).build();
   client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipAddress), port));

   return client;
}

public static void closeClient(){
    if(client != null){
        client.close();
     }
  }
}

Before 5.0.0 version (2.* versions), we could use TranportClient that was in core. We could connect to elasticsearch that were before 5.0.0 as follows:


public static Client getElasticsearchClient(String clustername, String ipAddress, int port) throws UnknownHostException{

     if(client != null){
          return client;
     }

     Settings settings = Settings.builder().put("cluster.name", clustername).build();
     client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ipAddress), port));

    return client;
}

For more: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s