使用节点客户端索引数据时获取异常

时间:2015-03-18 08:05:01

标签: elasticsearch

我之前使用过传输客户端工作正常。我想现在使用节点客户端。我尝试了以下方法:

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索引中看到数据。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

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

客户端为真时从节点客户端文档

  

节点是否是客户端节点,这意味着它不会保留任何数据   (node.data设置为false)和其他优化不同   模块。

//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();