# Unable to Connect to Elasticsearch 6.1.0 via the javaclient

**URL:** <https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358>\
**Category:** Elasticsearch\
**Created:** [December 19, 2017, 6:30am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358 "2017-12-19T06:30:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallavJha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pallavjha/32/25775_2.png) [@pallavJha](https://discuss.elastic.co/u/pallavJha)\
**Post date:** [December 19, 2017, 6:30am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/1 "2017-12-19T06:30:46Z")

</div>

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,

```auto
    /**
     * 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

```auto
#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:

```auto
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,

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

```

Java Client Code:

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 6:50am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/2 "2017-12-19T06:50:16Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). 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.

---

<div class="post-metadata">

**Author:** ![pallavJha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pallavjha/32/25775_2.png) [@pallavJha](https://discuss.elastic.co/u/pallavJha)\
**Post date:** [December 19, 2017, 7:00am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/3 "2017-12-19T07:00:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 7:58am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/4 "2017-12-19T07:58:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pallavJha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pallavjha/32/25775_2.png) [@pallavJha](https://discuss.elastic.co/u/pallavJha)\
**Post date:** [December 19, 2017, 8:19am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/5 "2017-12-19T08:19:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pallavJha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pallavjha/32/25775_2.png) [@pallavJha](https://discuss.elastic.co/u/pallavJha)\
**Post date:** [December 19, 2017, 8:21am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/6 "2017-12-19T08:21:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2017, 8:32am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/7 "2017-12-19T08:32:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pallavJha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pallavjha/32/25775_2.png) [@pallavJha](https://discuss.elastic.co/u/pallavJha)\
**Post date:** [December 25, 2017, 6:57pm UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/8 "2017-12-25T18:57:01Z")

</div>

Hi @dadoonet`Dadoonet`,

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 26, 2018, 11:59am UTC](https://discuss.elastic.co/t/unable-to-connect-to-elasticsearch-6-1-0-via-the-javaclient/112358/10 "2018-01-26T11:59:53Z")

</div>

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