# How to use the RestHighLevelClient for the cloud?

**URL:** <https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027>\
**Category:** Elasticsearch\
**Created:** [March 19, 2019, 5:17pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027 "2019-03-19T17:17:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)\
**Post date:** [March 19, 2019, 5:17pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/1 "2019-03-19T17:17:15Z")

</div>

I use the Java RestHighLevelClient, and it works fine with localhost elastic search (in which I pass "localhost" and 9200, "http") like the example in here

[https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-getting-started-initialization.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-getting-started-initialization.html)

But how do I use this for elastic search cloud? and I suppose the host would be ".us-east-1.aws.found.io" and my port will be "9203" and scheme is always "https"?

There is a user/password associated with the cloud version... how to pass that along?

---

<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:** [March 19, 2019, 5:58pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/2 "2019-03-19T17:58:20Z")

</div>

This is happening in the Low Level client. See [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_basic\_authentication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html)

Here is a sample code I'm using:

> <https://github.com/dadoonet/fscrawler/blob/master/elasticsearch-client/elasticsearch-client-v6/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/v6/ElasticsearchClientV6.java#L406-L420>

Note that `decodeUrl()` is basically returning the host url like `https://XYZ.es.io:9243`.

---

<div class="post-metadata">

**Author:** ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)\
**Post date:** [March 20, 2019, 7:13pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/3 "2019-03-20T19:13:01Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)\
**Post date:** [March 21, 2019, 5:32pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/4 "2019-03-21T17:32:14Z")

</div>

The following works perfectly fine in a little standalone Java Application (where the constants are pointing to my instance)...

```
    RestHighLevelClient restHighLevelClient = null;
    if (!"localhost".equals(ELASTIC_HOST)) {

        RestClientBuilder builder = RestClient
                .builder(new HttpHost(ELASTIC_HOST, Integer.parseInt(ELASTIC_PORT), ELASTIC_SCHEME));

        // if not localhost, specify username and password
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTIC_USER, ELASTIC_PASSWORD));

        builder.setHttpClientConfigCallback(
                httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(40000).setSocketTimeout(50000)).setMaxRetryTimeoutMillis(60001);

        restHighLevelClient = new RestHighLevelClient(builder);

    } else {
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost(ELASTIC_HOST, Integer.parseInt(ELASTIC_PORT), "http")));
    }

    try {
        LOGGER.info("ping");
        boolean pingResult = restHighLevelClient.ping(RequestOptions.DEFAULT);
        LOGGER.info("pingResult=" + pingResult);
    } catch (Exception e) {
        LOGGER.error("Can't even ping: ",e);
    }

```

However, inside a Web application (in a data access object)... this ping will fail (and any search, show indices etc would also fail) with a Timeout.

```auto
03-21-2019 12:20:55 ERROR ElasticsearchDAO:388 - Can't even ping: 
java.io.IOException: listener timeout after waiting for [60001] ms
	at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:899)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:227)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1256)
	at org.elasticsearch.client.RestHighLevelClient.ping(RestHighLevelClient.java:372)

```

Any idea what extra steps a Java web application may need to access elastic search on cloud?

---

<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:** [March 22, 2019, 4:15pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/5 "2019-03-22T16:15:58Z")

</div>

The only thing that I can think about would be a conflicting dependency or something like this.  
What is the web server you are deploying this app to?

---

<div class="post-metadata">

**Author:** ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)\
**Post date:** [March 22, 2019, 4:29pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/6 "2019-03-22T16:29:11Z")

</div>

Thanks. I am using Wildfly 10. I also tried using "http" with 9200, and same result...

---

<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:** [March 22, 2019, 4:31pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/7 "2019-03-22T16:31:35Z")

</div>

Is there any way to print debug all classes to see what is happening?  
Or at least `org.elasticsearch` and `org.apache` packages?

---

<div class="post-metadata">

**Author:** ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)\
**Post date:** [March 25, 2019, 8:18pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/8 "2019-03-25T20:18:32Z")

</div>

Thanks @dadoonet, yes, conflicting maven artifact was the culprit.  
I was able to use mvn dependency:tree to figure that elastic search uses a particular version of apache httpcomponent which uses a particular version of commons-codec... and I had another place that uses a different version which conflicted 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:** [April 22, 2019, 8:18pm UTC](https://discuss.elastic.co/t/how-to-use-the-resthighlevelclient-for-the-cloud/173027/9 "2019-04-22T20:18:34Z")

</div>

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