gpt4 book ai didi

exception - 为什么我的 elasticsearch 无法在 JAVA API 中构建 transportclient?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:43 35 4
gpt4 key购买 nike

我在 Java 中有一个简单的 elasticsearch 代码是这样的:

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

这很简单,但我在以下行中遇到错误:

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

它显示如下错误信息:

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)

所以,你能给我建议吗,我一直在寻找解决方案,但找不到。有没有我必须实现的配置?我的 elasticsearch 服务器已正确运行,它可以执行索引并从命令提示符获取查询。谢谢...

最佳答案

这段代码对我有用。这是版本问题。 TransportClient 的默认端口是 9300 并且代码不使用 Elasticsearch 2.0 编译

使用这个:

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

客户端客户端 = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));

引用:Here's the link

关于exception - 为什么我的 elasticsearch 无法在 JAVA API 中构建 transportclient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532411/

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