【问题标题】:Create index in Elastic Search by Java API通过 Java API 在 Elastic Search 中创建索引
【发布时间】:2020-03-03 06:00:15
【问题描述】:

我使用以下代码在 Elastic Search 中创建索引, 默认 JAVA API:

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 服务:

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

通过使用 REST API,我可以创建索引。默认情况下 JAVA API, 我收到以下异常。

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)

请指导我在哪里出错。提前致谢

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    TransportClient 的端口(通过 java API)与 Http 不同 默认情况下,transportClient 端口为 9300

    【讨论】:

    • 感谢您的回答。我仍然遇到同样的异常。
    • 现在可以正常使用了。我在使用 9300 时犯了一个小错误。但是在配置中我更新了端口号。
    【解决方案2】:

    使用副本和分片设置:

    Settings indexSettings = ImmutableSettings.settingsBuilder()
                     .put("number_of_shards", 1)
                     .put("number_of_replicas", 1)
                     .build();
    CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings);
    client.admin().indices().create(indexRequest).actionGet();
    

    【讨论】:

    【解决方案3】:

    既然你有你的客户,那么你不应该这样做吗:

    CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet();
    

    【讨论】:

    • 我只想补充一点,createIndexRequest 方法是 Requests.createIndexRequest("test1")
    【解决方案4】:

    使用 JAVA High-Level Rest Client 下面的代码可以用来创建索引。

     CreateIndexRequest request = new CreateIndexRequest("users");
            request.settings(Settings.builder()
                    .put("index.number_of_shards", 3)
                    .put("index.number_of_replicas", 2)
            );
            Map<String, Object> message = new HashMap<>();
            message.put("type", "text");   
    
            Map<String, Object> properties = new HashMap<>();
            properties.put("userId", message);
            properties.put("name", message);
    
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            request.mapping(mapping);
    
            CreateIndexResponse indexResponse = client.indices().create(request, RequestOptions.DEFAULT);
            System.out.println("response id: "+indexResponse.index());
    

    如需进一步参考,请阅读: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-create-index.html#java-rest-high-create-index

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2020-04-25
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      相关资源
      最近更新 更多