Unable to connect to Elastic search cloud with Transport client API

Please help me to connect to the elastic cloud from the transport client

pom.xml

 <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>6.0.0</version>
    </dependency>

Code in JAVA Spring BOOT

      boolean enableSsl = true;
    Settings settings = Settings.builder()
            .put("client.transport.nodes_sampler_interval", "5s")
            .put("client.transport.sniff", false)
            .put("transport.tcp.compress", true)
            .put("cluster.name", clusterName)
            .put("xpack.security.transport.ssl.enabled", enableSsl)
            .put("request.headers.X-Found-Cluster", "${cluster.name}")
            .put("xpack.security.user", System.getProperty("xpack.security.user"))
            .put("xpack.security.transport.ssl.verification_mode", insecure ? "none" : "full")
            .build();

    String hostname = "7c12baf8da6b4c1da553c7557a004844.us-east-1.aws.found.io";
    try {

        System.out.println("CONNECT");
        TransportClient myclient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(hostname), 9343));

    }catch (Exception e){
        e.printStackTrace();
    }

getting the exception

java.lang.NullPointerException
at org.elasticsearch.node.InternalSettingsPreparer.replacePromptPlaceholders(InternalSettingsPreparer.java:160)
at org.elasticsearch.node.InternalSettingsPreparer.finalizeSettings(InternalSettingsPreparer.java:153)
at org.elasticsearch.node.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:52)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:97)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:125)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:259)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:125)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:111)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:101)
at com.fms.eventurbo.etelastic.service.EventService.<init>(EventService.java:79)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1164)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1244)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)

What gives

GET /

First you need to use the same client version as the server. 6.5.4 and not 6.0.0.

Getting another exception with the below code changes

 boolean enableSsl = true;
    String clusterId = "7c12baf8da6b4c1da553c7557a004844"; // Your cluster ID here

    String region = "us-east-1";
    Settings settings = Settings.builder()
            .put("client.transport.nodes_sampler_interval", "5s")
            .put("client.transport.sniff", false)
            .put("transport.tcp.compress", true)
            .put("cluster.name", clusterName)
            .put("xpack.security.transport.ssl.enabled", enableSsl)
            .put("request.headers.X-Found-Cluster", clusterId)
            .put("xpack.security.user", "elastic:pass")
            .put("xpack.security.transport.ssl.verification_mode", insecure ? "none" : "full")
            .build();
    String hostname = clusterId + "." + region + ".aws.found.io";
    try {

        System.out.println("CONNECT");
        TransportClient myclient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(hostname), 9343));

    }catch (Exception e){
        e.printStackTrace();
    }

exception

java.lang.IllegalArgumentException: unknown setting [xpack.security.transport.ssl.enabled] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:293)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:256)
at org.elasticsearch.common.settings.SettingsModule.<init>(SettingsModule.java:135)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:137)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:259)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:125)

Kindly Help @dadoonet

@dadoonet. Thank you, I will update the same but it was working fine with the local elastic server

Then you need to read: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/setup-xpack-client.html

https://www.elastic.co/guide/en/elastic-stack-overview/6.5/java-clients.html

That is already added to the pom.xml file :disappointed_relieved: The version is correct or not please

	<dependency>
		<groupId>org.elasticsearch.client</groupId>
		<artifactId>x-pack-transport</artifactId>
		<version>6.5.4</version>
	</dependency>

error

java.lang.IllegalArgumentException: unknown setting [xpack.security.transport.ssl.enabled] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:476)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:421)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:392)
at org.elasticsearch.common.settings.AbstractScopedSettings.validate(AbstractScopedSettings.java:363)
at org.elasticsearch.common.settings.SettingsModule.<init>(SettingsModule.java:148)

@dadoonet I could see the below item in the documentation

Kindly have a look and guide me to resolve the same

 java.lang.IllegalArgumentException: empty text

The exception is typically seen when a https request is made to a node that is not using https. If https is desired, please ensure the following setting is in elasticsearch.yml:

xpack.security.http.ssl.enabled: true

https://www.elastic.co/guide/en/x-pack/5.4/security-troubleshooting.html

Don't read old documentation. https://www.elastic.co/guide/en/x-pack/5.4/security-troubleshooting.html is for 5.4.

There is an example of a 6.2 version which I hope is still accurate:

But you should NOT be using the transport client anyway but the REST Client instead.

1 Like

Thank you very much I could connect with the below code

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 client() {

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(userName, password));

    RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

    RestHighLevelClient client = new RestHighLevelClient(builder);


    return client;

}

}

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