Connect to Elasticsearch using Java High Level Client

Hi, I'm new to Elasticsearch. I'm trying to connect Java to Elasticsearch using Java High Level Client. However, I always got an IOException. I was sure that my Elasticsearch is up and running. Here is the code I used. My Elasticsearch and high level client are both in version 7.0.0

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "mypassword"));

        RestClientBuilder builder = RestClient
                .builder(new HttpHost("myclustername.us-east-1.aws.found.io", 9243, "http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        GetRequest getRequest = new GetRequest("item", "1");
 

try {
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
    e.printStackTrace();
}

Below is the stack trace:
2019-04-23T01:12:08.072466+00:00 app[web.1]: org.apache.http.ConnectionClosedException: Connection is closed

2019-04-23T01:12:08.074210+00:00 app[web.1]: at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:794)

2019-04-23T01:12:08.074276+00:00 app[web.1]: at org.elasticsearch.client.RestClient.performRequest(RestClient.java:225)

2019-04-23T01:12:08.074352+00:00 app[web.1]: at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)

2019-04-23T01:12:08.074422+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)

2019-04-23T01:12:08.074492+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1403)

2019-04-23T01:12:08.074571+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1373)

2019-04-23T01:12:08.074637+00:00 app[web.1]: at org.elasticsearch.client.RestHighLevelClient.get(RestHighLevelClient.java:699)

2019-04-23T01:12:08.074702+00:00 app[web.1]: at edu.lehigh.cse280.swap.app.App.main(App.java:110)

2019-04-23T01:12:08.074855+00:00 app[web.1]: Caused by: org.apache.http.ConnectionClosedException: Connection is closed

2019-04-23T01:12:08.074947+00:00 app[web.1]: at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:356)

2019-04-23T01:12:08.075024+00:00 app[web.1]: at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261)

2019-04-23T01:12:08.075099+00:00 app[web.1]: at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)

2019-04-23T01:12:08.075165+00:00 app[web.1]: at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)

2019-04-23T01:12:08.075233+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)

2019-04-23T01:12:08.075308+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)

2019-04-23T01:12:08.077773+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)

2019-04-23T01:12:08.077840+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)

2019-04-23T01:12:08.077930+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)

2019-04-23T01:12:08.078001+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)

2019-04-23T01:12:08.078073+00:00 app[web.1]: at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)

2019-04-23T01:12:08.078138+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:748)

I tried with curl command
curl -X GET -u elastic:mypassword "https://myclustername.us-east-1.aws.found.io:9243/item/_doc/1"
And it worked well.

This is my first time posting, please forgive me if my question is formatted incorrectly :slight_smile:

Thanks!

Try with https instead of http.

OMG you are a life saver! This problem has bothered me for weeks! Thank you very much!

1 Like

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