1

I was trying to connect to my elasticsearch cluster using RestHighLevelClient but it seems to be not working for me. Following is the code snippet I used.

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Base64;

@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;


    @Bean(destroyMethod = "close")
    public RestHighLevelClient esClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username,     password));

        System.out.println("header " +  Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
        String encodedBytes = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, 9243,"https"));
    /*Header[] headers = {new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
            new BasicHeader("Authorization", "Basic " + encodedBytes)};
    */
        builder.setHttpClientConfigCallback(
            httpClientBuilder ->     httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    //builder.setDefaultHeaders(headers);
        System.out.println("password" +     credentialsProvider.getCredentials(AuthScope.ANY).getPassword());
    System.out.println("name" +     credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName());
    return new RestHighLevelClient(builder);

    }

}

I tried using the same set of hostname, port and credentials (Base 64 encoded string of "username:password") with cURL and it connects, but somehow programmatically it's giving my UnknownHostException, which is pretty weird, help is appreciated.

EDIT I'm using 9243 which is working with cURL and also I tried 9200, 9300 just in case but to no avail.

Deepesh
  • 41
  • 1
  • 2
  • 9

1 Answers1

0

One small but important correction. When using the default setup of Elastic Search, you should be connecting to port 9200.

Please read official documentation: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html

  • It's 9243 for me, and this one I just tried after reading one elastic.co question thread. So port 9243 is working for me using cURL. But the issue with java client is still there. – Deepesh Nov 08 '19 at 13:05
  • Can you install/test the Chrome ElasticSearch Head plugin? It allows to connect to ES and supports browsing your indexes. The other thing you'll have to verify is the version of your org.elasticsearch dependency in your pom. You have to make sure this version is compatible with the version of the ES you are trying to connect with. – Wim Van den Brande Nov 08 '19 at 13:19
  • Deepesh, on the other hand, you are getting an UnknownHostException so are you entirely confident the name of your host is correct? Log your Java-host-value and compare with the value you are using in combination with curl. – Wim Van den Brande Nov 08 '19 at 13:37
  • Yes if they weren't correct why'd my cURL command work? I was able to create an index, put data in it. But it's just java client that's causing issues. – Deepesh Nov 08 '19 at 13:55