gpt4 book ai didi

使用 TransportCilent 的 Java ElasticSearch 示例

转载 作者:行者123 更新时间:2023-11-30 09:09:00 27 4
gpt4 key购买 nike

下面的代码解释了Java和ElasticSearch的通信
使用传输客户端。当我运行时,我得到 org.elasticsearch.nodedisconnectedException。任何关于 Java Elasticsearch 的明确示例将不胜感激,或者任何人都可以帮助我找到解决方案。

public static Client function() {
Settings setting = ImmutableSettings.settingsBuilder().put("cluster.name", "localtestsearch").build();
client = new TransportClient();
client = client.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
return client;

}

public static void main(String[] args) {

System.out.println("In Main Method");

try {
System.out.println("In try block");
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress("localhost",
9300));


System.out.println("Connected");

client.close();

Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true).build();
TransportClient client1 = new TransportClient(settings);


org.elasticsearch.node.Node node = nodeBuilder().node();
System.out.println("Node " + node);

Client client = node.client();

System.out.println("calling Client from Node " + client);

node.close();
System.out.println("Node CLosed");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

最佳答案

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class Test {
public static void main(String[] args) {
TransportClient client = new TransportClient();

for(String host: client.settings().getAsArray("transport.client.initial_nodes")) {
int port = 9300;

// or parse it from the host string...
String[] splitHost = host.split(":", 2);
if(splitHost.length == 2) {
host = splitHost[0];
port = Integer.parseInt(splitHost[1]);
}

client.addTransportAddress(new InetSocketTransportAddress(host, port));
}

// ...
}
}

关于使用 TransportCilent 的 Java ElasticSearch 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23261828/

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