Search code examples
javaelasticsearchelastic-stackelasticsearch-5

Unable to connect my java application to the Elastic Stack Instance


I have set up my Elastic Cloud Service through Google Cloud and have set up an Elastic Search Instance.

I can upload my data to Elastic search and query my data just fine. However, when I try to connect to the Elastic Search Instance through my Java Client, I keep getting a 'java.io.IOException' and a 'java.net.UnknownHostException' exceptions.

24-Jun-2020 18:55:52.657 SEVERE [http-nio-8181-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception
     java.io.IOException: <Elastic Search endpoint>
        at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:828)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:248)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1611)
        
    Caused by: java.net.UnknownHostException: <Elastic Search Endpoint>
        at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
        at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
        at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:664)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:635)
        at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
        at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:377)
        at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:129)
        at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:244)
        ... 49 more

And my Java Code:

String ELASTIC_SEARCH_USER_NAME = "elastic";
String ELASTIC_SEARCH_PASSWORD = <Password>;
String ELASTIC_SEARCH_ENDPOINT_URL = "https://92d5f385db294fb4b7ff335201d0a854.asia-northeast1.gcp.cloud.es.io";
Integer ELASTIC_SEARCH_PORT = 9243;

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(ELASTIC_SEARCH_USER_NAME, ELASTIC_SEARCH_PASSWORD));

RestClientBuilder builder = RestClient.builder(new HttpHost(ELASTIC_SEARCH_ENDPOINT_URL, ELASTIC_SEARCH_PORT, "https"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                        }
                    });

RestHighLevelClient highLevelClient = new RestHighLevelClient(builder);

Strangely, I have tried pinging my endpoint url in the command line but my cmd is unable to ping the url.

Is there something I need to set up in my Elastic Stack Console for my Java Client to request queries?

Thank you!


Solution

  • Asking the guys over at elastic.co forums, they suggested that I drop the "https://" portion from the ELASTIC_SEARCH_ENDPOINT_URL.

    String ELASTIC_SEARCH_ENDPOINT_URL = "https://92d5f385db294fb4b7ff335201d0a854.asia-northeast1.gcp.cloud.es.io";
    

    to

    String ELASTIC_SEARCH_ENDPOINT_URL = "92d5f385db294fb4b7ff335201d0a854.asia-northeast1.gcp.cloud.es.io";
    

    and keeping the rest of the code, it worked!

    The forum post I made if anyone wants to take a look