Why was my elasticsearch unable to create a transportclient in the JAVA API?

I have simple elasticsearch code in Java, for example:

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 is very simple, but I got an error in the following line:

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

An error message appears as follows:

 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 an offer, I was looking for a solution, and I can’t find it. Is there some kind of configuration that I have to implement? My elasticsearch server is working correctly, it can execute an index and receive requests from the command line. Thanks...

+5
source share
1 answer

This code works for me. This is a version issue. The default port of TransportClient is 9300, and the code is not compiled with Elasticsearch 2.0

Use this:

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));

Contact: Here is the link

+2
source

Source: https://habr.com/ru/post/1243486/


All Articles