gpt4 book ai didi

java - 使用 PreBuiltTransportClient 调用 elasticsearch

转载 作者:行者123 更新时间:2023-12-02 10:34:19 24 4
gpt4 key购买 nike

我创建一个新的正在运行的 docker 弹性实例:

docker run --name i3-ps-elastic -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:5.4.3

它对 http 调用响应良好:

$ curl localhost:9200
{
"name" : "_wqolsQ",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "m2Mh9oXaSbiPF2UQprrB-Q",
"version" : {
"number" : "5.4.3",
"build_hash" : "eed30a8",
"build_date" : "2017-06-22T00:34:03.743Z",
"build_snapshot" : false,
"lucene_version" : "6.5.1"
},
"tagline" : "You Know, for Search"
}

注意 cluster_name 如何在客户端和服务器中设置为相同的值。

然后我运行以下测试:

@Test
public void checkElasticConnection() throws UnknownHostException {

Settings.Builder settings = Settings.builder().put("cluster.name", "docker-cluster");
TransportClient client = new PreBuiltTransportClient(settings.build());
client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

assertThat(client).isNotNull();
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().get();
assertThat(clusterHealthResponse).isNotNull();
}

我得到:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{hRbm7zJ1TR6p_KdmohkCFQ}{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:60)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:378)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
at org.elasticsearch.client.support.AbstractClient$ClusterAdmin.execute(AbstractClient.java:706)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53)
...

最佳答案

对我来说,问题是服务器和客户端之间的版本不匹配。我的服务器是 5.4.3 和客户端 6.3.0。我通过删除 docker 实例并运行匹配的版本解决了这个问题:

docker run --name i3-ps-elastic -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:6.3.0

关于java - 使用 PreBuiltTransportClient 调用 elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53396894/

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