Index creation in elastic search through Java API

I use the following code to create an index in elastic search,

Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name","myClusterName").put("client.transport.sniff",true).build();
    Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost",9200));
    CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("test1");
    CreateIndexResponse response = createIndexRequestBuilder.execute().actionGet();
    System.out.println(response.isAckNowledged());

Rest services:

HttpURLConnection con = null;
    try
    {
        String url = "http://localhost:9200/test2";

        URL resturl = new URL(url);
        con = (HttpURLConnection) resturl.openConnection();

        con.setDoOutput(true);
        con.setRequestMethod("PUT");
        BufferedReader in = null;
        try
        {
            if (con.getInputStream() != null)
            {
                in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            }
        }
        catch (IOException e)
        {
            if (con.getErrorStream() != null)
            {
                in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            }
        }
        if (in == null)
        {
            throw new Exception("Unable to read response from server");
        }
        StringBuffer decodedString = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null)
        {
            decodedString.append(line);
        }
        in.close();
        System.out.println("4");
        Integer responseCode = con.getResponseCode();
        System.out.println(responseCode);
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        if (con != null)
        {
            con.disconnect();
        }
    }

By using the rest API, I can create indexes By default, Java API, I get the following exception

org.elasticsearch.client.transport.NoNodeAvailableException: No node available
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202)
at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:85)
at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:200)
at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:206)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57)
at ElasticSearch.createIndex(ElasticSearch.java:121)
at ElasticSearch.main(ElasticSearch.java:157)

Please guide me where I make mistakes Thank you in advance

Solution

The port of transportclient (through Java API) is different from http. By default, the port of transportclient is 9300

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>