gpt4 book ai didi

elasticsearch - 使用节点客户端在索引数据中获取异常

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

我使用过运输客户端,在此之前效果很好。我现在想使用节点客户端。我尝试了以下方法:

package es.code;

import org.apache.log4j.Logger;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;

import static org.elasticsearch.node.NodeBuilder.*;

import java.io.IOException;

public class elasticsearch {

private elasticsearch() {
}

public static void main(String[] args) throws IOException {
System.out.println("Hello, World");
Client client = null;

Node node = nodeBuilder().clusterName("sample_test").client(true).node();
client = node.client();
String json = "{" +
"\"user\":\"prachi\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";

IndexResponse response = client.prepareIndex("estest", "testing")
.setSource(json)
.execute()
.actionGet();

System.out.println(response.getId());
node.close();
}

}

我收到以下异常:
Exception in thread "main" org.elasticsearch.discovery.MasterNotDiscoveredException: waited for [1m]
at org.elasticsearch.action.support.master.TransportMasterNodeOperationAction$4.onTimeout(TransportMasterNodeOperationAction.java:164)
at org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener.onTimeout(ClusterStateObserver.java:239)
at org.elasticsearch.cluster.service.InternalClusterService$NotifyTimeout.run(InternalClusterService.java:520)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

我做错了什么?如果删除client(true),程序将成功运行。但是我没有在ES索引中看到数据。

正确的方法是什么?

最佳答案

我刚刚玩过您的代码,它运行得很好,要使此代码正常工作,您必须在群集上运行主节点(名称为sample_test)。当您拥有client(true)时,ES将创建一个传输客户端实例,该实例将远程连接到正在运行的集群(不加入集群)。

当客户端为true时,从节点客户端文档中获取

Is the node going to be a client node which means it will hold no data (node.data is set to false) and other optimizations by different modules.


//Master node should be running. Works as a client. 
nodeBuilder().clusterName("sample_test").client(true).node()

//Spin up individual node instance.
nodeBuilder().clusterName("sample_test").node()

如果您还需要为节点客户端配置数据存储,则以下代码可能会为您提供帮助。
Settings settings = ImmutableSettings.settingsBuilder().put("path.data", "D:\\ESDATA\\").build();
nodeBuilder().settings(settings);
Node node = nodeBuilder().clusterName("sample_test").data(true).settings(settings).node();

关于elasticsearch - 使用节点客户端在索引数据中获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116848/

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