java.net.UnknownHostException using Java Rest client 5.6.3

I keep getting java.net.UnknownHostException for connecting to my host elasticsearch.
I have not completed the actual search body yet, because I am getting unknown host error.

I figure I can solve that problem later.

Here is the code, any reason why I cannot reach my cluster?

Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
                    new BasicHeader("Role", "Read") };
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "password"));

RestClient lowLevelRestClient = RestClient.builder(
            new HttpHost("https://xxxxxxxxx:9243")).setDefaultHeaders(headers).build();



RestHighLevelClient client =
    	    new RestHighLevelClient(lowLevelRestClient);

String thePath = "index_name";
    try {
    	thePath = URLEncoder.encode(thePath, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    } 

SearchRequest searchRequest = new SearchRequest(thePath);
SearchResponse searchResponse = null;
    try {
    	searchResponse = client.search(searchRequest);
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }

    		
    	}

I believe you should create an http host like this instead:

new HttpHost("xxxxxxxxx", 9243, "https")

See https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html

same error.
Any ideas?

Did you apply everything which is in https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html ?

What are the plugins you are using? Can you share elasticsearch logs?

No, But I am a bit confused.

The docs say to instantiate a high level rest object with a low level rest object.
Problem is the documentation from your link doesn't use the low level rest client at all.

Should I just use what's in your link?

How can I share the logs with you?
Is there an export?

When you start elasticsearch, it produces logs. Just share them here. Don't forget to format them.

Problem is the documentation from your link doesn't use the low level rest client at all.

Isn't that RestClientBuilder builder = RestClient.builder(...) a Low Level client?

Here is the doc:

Also, I am using elastic cloud.
My logs appear like this:

When I click into the logs, there's nothing useful.

"No results found in the last week's logs."

Last time I built such a client for elastic cloud service, I wrote:

I did not test it it recently though.

Quick question: are you able to do it from the command line using curl for example?

I mean are you sure you have the rights to connect to the cluster?

I have not tried CURL.

This is what I have now:

Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
                new BasicHeader("Role", "Read") };
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
		


		RestClient r = RestClient.builder(new HttpHost("xxxxxxxxxxxxxx.us-east-1.aws.found.io",9243,"https")).setDefaultHeaders(headers).build();
		
		
		Map<String, String> params = Collections.singletonMap("pretty", "true");
		Response response = r.performRequest("GET", "/", params);
		
		System.out.println(response.toString());
		
		try {
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

I get the following error:

Exception in thread "main" org.elasticsearch.client.ResponseException: GET https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true: HTTP/1.1 401 Unauthorized
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "action [cluster:monitor/main] requires authentication",
"header" : {
"WWW-Authenticate" : "Basic realm="security" charset="UTF-8""
}
}
],
"type" : "security_exception",
"reason" : "action [cluster:monitor/main] requires authentication",
"header" : {
"WWW-Authenticate" : "Basic realm="security" charset="UTF-8""
}
},
"status" : 401
}

at org.elasticsearch.client.RestClient$1.completed(RestClient.java:354)
at org.elasticsearch.client.RestClient$1.completed(RestClient.java:343)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
at java.lang.Thread.run(Unknown Source)

Can you try with curl?

Curls works:

curl -u username:password 'https://xxxxxxxxxxxxxxxxx.us-east-1.aws.found.io:9243/index_name/_search?scroll=1m&size=50&pretty' -d '{"query" : {"match_all" : {}}}'

Can you also curl https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true

Same user as the ones you put in the rest Client

Yes, works fine:

{
"name" : "instance-0000000000",
"cluster_name" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"cluster_uuid" : "xxxxxxxxxxxxxxxxxxxxxxxxx",
"version" : {
"number" : "5.6.3",
"build_hash" : "xxxxxx",
"build_date" : "2017-10-06T20:33:39.012Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}

FYI:

My Java is run from my desktop.

The curl is run from an EC2 instance, ubuntu.

Can you try from the same platform please?

Sure, this is what I get:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.elasticsearch.client.ResponseException: GET https://xxxxxxxxxxxx:9243/?pretty=true: HTTP/1.1 401 Unauthorized
{
  "error" : {
    "root_cause" : [
      {
        "type" : "security_exception",
        "reason" : "action [cluster:monitor/main] requires authentication",
        "header" : {
          "WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
        }
      }
    ],
    "type" : "security_exception",
    "reason" : "action [cluster:monitor/main] requires authentication",
    "header" : {
      "WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
    }
  },
  "status" : 401
}

        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:354)
        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:343)
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
        at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
        at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
        at java.lang.Thread.run(Thread.java:748)

Problem solved!
@dadoonet thanks for the help, again!!!

Final issue to solve was my Authorization header.

Here is the working code:

String encodedBytes = Base64.getEncoder().encodeToString("username:password".getBytes());
		Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
				new BasicHeader("Authorization","Basic "+encodedBytes) };
		
		RestClient r = RestClient
				.builder(new HttpHost("xxxxxxxxxxx", 9243, "https"))
				.setDefaultHeaders(headers).build();

		Map<String, String> params = Collections.singletonMap("pretty", "true");
		Response response = r.performRequest("GET", "/", params);

		System.out.println(response.toString());

		try {
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Glad you solved it. I wanted to test it but traveling ATM so hard to do it. :slight_smile:

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