gpt4 book ai didi

java - Elasticsearch无节点可用异常

转载 作者:行者123 更新时间:2023-12-02 12:22:41 25 4
gpt4 key购买 nike

我对 Elasticsearch 概念非常陌生。我正在尝试使用 Elasticsearch 构建一个简单的应用程序。

public class App 
{
public static void main( String[] args )
{
try {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

ElasticOperations.addDocToIndex(client, ElasticUtility.getJsonMap("Sandesha", "male", "jsandesh08@gmail.com", "Hassan"));
System.out.println("Doc added");
client.close();
} catch (UnknownHostException e) {

e.printStackTrace();
}

}
}

但是,当尝试运行此程序时,我收到 NoNodeAvailableException

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{w1bSLAN-TFWUHE1PA3qDeQ}{localhost}{127.0.0.1:9300}]] at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363) at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) at com.sandesha.elasticsearch.Elastic.ElasticOperations.addDocToIndex(ElasticOperations.java:11) at com.sandesha.elasticsearch.Elastic.App.main(App.java:27)

请帮我解决这个问题。我的 ElasticOperations 类是,

public class ElasticOperations {

public static void addDocToIndex(TransportClient client, Map<String,Object> jsonMap)
{
client.prepareIndex("customers", "personal").setSource(jsonMap).execute().actionGet();
}

}

我的 ElasticUtility 类看起来像,

public class ElasticUtility {

public static Map<String, Object> getJsonMap(String name, String gender, String email, String city)
{
Map<String,Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("name", name);
jsonMap.put("gender", gender);
jsonMap.put("email", email);
jsonMap.put("city", city);

return jsonMap;
}
}

谢谢。

最佳答案

我认为您的集群名称不匹配。如果您的集群名称不是“elasticsearch”,则可能是问题所在。

转到http://localhost:9200在浏览器中并获取您的集群名称。然后,尝试将集群名称属性添加到 TransportClient,如下所示:

Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch_foo").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

关于java - Elasticsearch无节点可用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com