Exception occurring when try to query a local elasticsearch node


(apanimesh061) #1
public class EsNode {
    private static File tempFile;
    private static File homeDir ;
    private static File confDir ;
    private static File dataDir ;
    private static Settings defaultSettings;

    private Node currentNode;

    public EsNode() {}

    public void init() throws IOException {
        try {
            tempFile = File.createTempFile("elasticSearchTest", "tmp");
            homeDir = new File(tempFile.getParent() + "/" + UUID.randomUUID().toString());
            confDir = new File(homeDir.getAbsolutePath() + "/config");
            dataDir = new File(homeDir.getAbsolutePath() + "/data");

            defaultSettings = ImmutableSettings
                    .settingsBuilder()
                    .put("node.http.enabled", false)
                    .put("http.enabled", false)
                    .put("cluster.name", "test_cluster")
                    .put("path.data", homeDir.getAbsolutePath())
                    .put("path.conf", confDir.getAbsolutePath())
                    .put("path.repo", homeDir.getAbsolutePath())
                    .put("script.disable_dynamic", false)
                    .put("index.refresh_interval", "1s")
                    .put("es.logger.level", "INFO")
                    .build();

            homeDir.deleteOnExit();
            confDir.deleteOnExit();
            dataDir.deleteOnExit();
            tempFile.deleteOnExit();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Client getClient() {
        return this.currentNode.client();
    }

    public void buildNode() {
        this.currentNode = nodeBuilder()
                .local(true)
                .data(true)
                .settings(defaultSettings)
                .build();
    }

    public void closeClient() {
        getClient().close();
    }

    public void ShutDown() {
        currentNode.close();
        if(currentNode.isClosed())
            System.out.println("Current running was closed.");
    }

    public void analyze() throws IOException {
         System.out.println(getClient().admin().indices().prepareExists("some_index").execute().actionGet().isExists());
    }

    public static void main(String[] args) throws IOException {
        EsNode esNode = new EsNode();
        esNode.init(); // create temporary stores/default settings
        esNode.buildNode();    // initialize node
        esNode.analyze();   // Error is raised here
        esNode.closeClient();
        esNode.ShutDown();
    }
}

This program raises this error:

Exception in thread "main" org.elasticsearch.common.util.concurrent.UncategorizedExecutionException: Failed execution
    at org.elasticsearch.action.support.AdapterActionFuture.rethrowExecutionException(AdapterActionFuture.java:90)
    at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:49)
    at edu.harvard.iq.text.core.service.node.EsNode.analyzeDocuments(EsNode.java:93)
    at edu.harvard.iq.text.core.service.node.EsNode.main(EsNode.java:132)
Caused by: java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.getValue(BaseFuture.java:292)
    at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.get(BaseFuture.java:279)
    at org.elasticsearch.common.util.concurrent.BaseFuture.get(BaseFuture.java:117)
    at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:45)
    ... 2 more
Caused by: java.lang.NullPointerException
    at org.elasticsearch.cluster.service.InternalClusterService.add(InternalClusterService.java:241)
    ... 12 more

What could be the issue?
Thanks.


(apanimesh061) #2

I found the issue.

buildNode() should have this.currentNode = nodeBuilder().local(true).data(true).settings(defaultSettings).build().start();.

To make it work I just added start() at the end.


(system) #3