Unable to Connect to Elasticsearch 6.1.0 via the javaclient

I'm able to connect to elastic search 6.1.0 and index, update, delete and query the documents via cURL and plugin head but I'm unable to connect to the same via the java client, i.e,

    /**
     * Elasticsearch
     */
    // https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
    compile group: 'org.elasticsearch', name: 'elasticsearch', version: '6.1.0'
    // https://mvnrepository.com/artifact/org.elasticsearch.client/transport
    **compile group: 'org.elasticsearch.client', name: 'transport', version: '6.1.0'**

dependencies. Below is elasticsearch.yml

#cluster.name: my-application
cluster.name: saasworthy-qa

node.name: qa-1

path.data: /var/lib/elasticsearch

path.logs: /var/log/elasticsearch

#network.host: 192.168.0.1
network.host: 10.10.10.2

#http.port: 9200

#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#discovery.zen.ping.unicast.hosts: ["10.10.10.2"]
#discovery.zen.minimum_master_nodes: 1

The error received via java client is:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{SLvQfw9FREWVhpwTBtjNWg}{X.X.X.X}{X.X.X.X:9300}]]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:360)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
    at com.sw.config.elastic.connection.factory.SaaSWorthyElasticsearchClientFactory.main

Also, netstat info,

netstat -antp | grep 9300
tcp6       0      0 10.10.10.2:9300         :::*                    LISTEN      19768/java 

Java Client Code:

public class SampleElasticsearchClientFactory extends BaseElasticsearchClientFactory {

    private static final Logger logger = LoggerFactory.getLogger(SampleElasticsearchClientFactory.class);


    private String clusterName = "saasworthy-qa" ;


    private String addresses = "X.X.X.X";


    private String ports = "9300";

    
    private void createConnection() throws UnknownHostException {
        
        logger.info("Initiating Elasticsearch Client creation.");
        super.createClient(addresses, ports, clusterName);
        logger.info("Elasticsearch Client created.");
    }

    
    private void closeConnection() {
        if (getClient() != null) {
            getClient().close();
        }
    }

    public static void main(String[] args) throws UnknownHostException {
        SampleElasticsearchClientFactory f = new SampleElasticsearchClientFactory();
        f.createConnection();
        SearchResponse response = f.getClient().prepareSearch("temp").setTypes("sample").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
        System.out.println(response);
    }
}


public class BaseElasticsearchClientFactory {

    private Client client;

    public Client getClient() {
        return client;
    }

    public void createClient(String addresses, String ports, String clusterName) throws UnknownHostException {

        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true).build();

        if (!StringUtils.isEmpty(addresses) && !StringUtils.isEmpty(ports)) {
            TransportClient transportClient = new PreBuiltTransportClient(settings);

            String[] address = addresses.split(",");
            String[] port = ports.split(",");

            for (int i = 0; i < address.length; i++) {
                transportClient = transportClient.addTransportAddress(
                        new TransportAddress(InetAddress.getByName(address[i].trim()), port[i] == null ? 9300 : Integer.valueOf(port[i].trim()))
                );
            }
            this.client = transportClient;
        }
    }

}

Kindly help me out in resolving the issue. Thanks.

Please format your code using </> icon as explained in this guide. It will make your post more readable.

Or use markdown style like:

```
CODE
```

Some thoughts:

  • Prefer using the Java REST Client instead
  • Share you code
  • Add missing libs (log4j2) although it's probably not the problem here.

Hi,
Code formatted, Java Client code added. Please have a look.

1 Like

The code looks good. But what is the IP of X.X.X.X?

The thing is that apparently your elasticsearch node listens on 10.10.10.2. If you are using anything else, I guess it won't work.

x.x.x.x is public IP. The client is not a part of elastic search server's network.

Also, I'm able to connect to elasticsearch via plugin head and curl. I am able to create the document and query it via the public I.P.

yeah but I'm pretty sure that if look at your logs the Transport layer is bound to the private IP. May be a routing issue, like you did not expose port 9300?

Hi @dadoonetDadoonet,

Merry Christmas!

I'm able to connect via the transport client after marking client.transport.sniff as false. As the server for dev environment, the cluster only contains one node. However, in one another project where elasticsearch-2.3.3 is used, with client.transport.sniff is set to true, along with the presence of only one node in the cluster, there weren't any issues relating to it. Please shed some light on it.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.