Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my elasticsearch failed to build transportclient in JAVA API?

I have an simple code of elasticsearch in Java like this :

public class TryElastic {

public static void main(String[] args) throws UnknownHostException {

    Map<String, Object> json = new HashMap<String, Object>();
    json.put("user","kimchy");
    json.put("postDate",new Date());
    json.put("message","trying out Elasticsearch");

    try {
        Settings settings = Settings.settingsBuilder()
            .put("cluster.name", "elasticsearch")
            .put("client.transport.sniff", true).build();

        TransportClient client = TransportClient.builder().settings(settings).build();
        //client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    } catch (NoNodeAvailableException e) {
        System.out.println(e.toString());
    }

    System.out.println("test");
}

}

It's very simple but I got an error in the following line :

TransportClient client = TransportClient.builder().settings(settings).build();

It display error message like this :

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.elasticsearch.node.internal.InternalSettingsPreparer.randomNodeName(InternalSettingsPreparer.java:198)
at org.elasticsearch.node.internal.InternalSettingsPreparer.finalizeSettings(InternalSettingsPreparer.java:177)
at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:64)
at org.elasticsearch.client.transport.TransportClient$Builder.build(TransportClient.java:119)
at TryElastic.main(TryElastic.java:64)

So, can you give me suggestion, I've looking for the solution, and I can't find that. Is there any configuration that I've to implement? My elasticsearch server have ran correctly, It can perform index and get queries from command prompt. Thanks...

like image 295
Kenny Basuki Avatar asked Feb 21 '16 04:02

Kenny Basuki


1 Answers

This code works for me. This is the version issue. Default port of TransportClient is 9300 and the code does not compile with Elasticsearch 2.0

Use this:

Settings settings = Settings.settingsBuilder() .put("cluster.name", clusterName).build();

Client client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));

Refer: Here's the link

like image 118
Priya Rajan Avatar answered Oct 18 '22 08:10

Priya Rajan