Unable to connect to Elasticsearch with X-Pack enabled

Hi

I am using Elasticsearch version 6.2.4.
After enabling xpack security, I am unable to connect my transport client to elasticsearch

TransportClient transportClient1 = new PreBuiltXPackTransportClient(
            Settings.builder().put("cluster.name", "Elastic-application")
                    .put("xpack.security.user", "elastic:pass@123")
                    .build());

    ipPortMapping = ExportMiscellaneousUtils.getURLDetails(connectionURL, ExportMiscellaneousUtils.DEFAULT_TCP_PORT_ELASTICSEARCH);
    for (Map.Entry<InetAddress, Integer> ipPortDetails : ipPortMapping.entrySet()) {
        transportClient1.addTransportAddresses(new TransportAddress(ipPortDetails.getKey(), ipPortDetails.getValue()));
    }

Following is the dependencies i have added to my maven
> <elasticsearch.version>6.2.4</elasticsearch.version>

        <repositories>
            <!-- add the elasticsearch repo -->
            <repository>
                <id>elasticsearch-releases</id>
                <url>https://artifacts.elastic.co/maven</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
        <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>x-pack-transport</artifactId>
                <version>6.0.0-beta2</version>
            </dependency>
        <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>${elasticsearch.version}</version>
            </dependency>

Any idea what is going wrong?

It would help it you share the error message.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.common.settings.Settings$Builder.put([Ljava/lang/Object;)Lorg/elasticsearch/common/settings/Settings$Builder;

This is the message I am getting.

Your code doesn't compile, you should fix that first..

It compiles just fine...
This is run time error:

Here is my code

Imports

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

And the connection code.

private void prepareClient(String connectionURL) {
        try {
            TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
                    .put("cluster.name", "myClusterName")
                    .put("xpack.security.user", "transport_client_user:x-pack-test-password")
                    .build())
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(connectionURL), 9300));

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

    }

Still, it calls a method that doesn't exists, java.lang.NoSuchMethodError. If I had to guess I would say something is wrong with Settings.builder() and how that is used. That said my java skills are very rusty, I really need to pick that up again..

Yes that's correct. it is unable to find the put method in Settings$builder().

But as per the documentations, above mentioned way is the way to do it...
Please ref: https://www.elastic.co/guide/en/elastic-stack-overview/current/java-clients.html

The first example here shows the code above...

I wonder what is wrong with my implementation?
Is it a incorrect dependency version.. or maybe an incorrect dependency itself!!

Hi,

Anyone with any input appreciated here.

may be use the right version instead of 6.0.0-beta2 of xpack client.

Hi David,
We check with right version but when we add the dependency in .pom file then not creating the .jar file, is there any other way for that.

Thanks

I'm not sure to understand.

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