2

I have used transport client before which works fine. I want to use node client now. I tried the following:

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

}

I am getting the following exception:

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)

What is it which I am doing wrong? If I remove client(true), the program runs successfully. But I don't see data in ES index.

What is the correct way to do it?

1 Answer 1

2

I just played with your code and it works perfectly fine.For this code to work you must have your master node running on your cluster (with name sample_test). When you have client(true) ES will create a Transport Client instance which will remotely connect to the running cluster (not joining the cluster).

From node client documentation when client 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() 

If you further need to configure data store for your node client following code might help you.

Settings settings = ImmutableSettings.settingsBuilder().put("path.data", "D:\\ESDATA\\").build();
nodeBuilder().settings(settings);
Node node = nodeBuilder().clusterName("sample_test").data(true).settings(settings).node();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.