ElasticSearch 错误“消息”:“请求大小超出 10485760 字节”

问题描述 投票:0回答:1

使用此方法在 ElasticSearch 中上传 50 mb 大小的巨大 JSON 字符串时 -

public static void postData(String json, String index, String type) {
    RestClient client = RestClient.builder(new HttpHost(testHostPreProd, 443, "https")).build();
    HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);

    Response response = client.performRequest("POST", "/" + index + "/" + type + "/_bulk",
            Collections.<String, String>emptyMap(), entity);

    client.close();
}

错误是

Exception in thread "main" org.elasticsearch.client.ResponseException: POST https:someURL/indexHere/typeHere/_bulk: HTTP/1.1 413 Request Entity Too Large
{"Message":"Request size exceeded 10485760 bytes"}

在可以发送 10k 批次的代码中放置一个计数器可能会起作用,但我不确定如何做到这一点。

关于如何处理这个问题有什么建议吗?

java elasticsearch elasticsearch-bulk
1个回答
0
投票

要下载大量记录,您可以使用 Scroll API:

https://www.elastic.co/guide/en/elasticsearch/reference/current/scroll-api.html

机制非常简单:

  1. 您创建一个查询并给出块大小,即单次下载的记录数 – 假设为 10。
  2. 它会返回您的 [10] 条记录和一个
    scroll_id
    ,它是您下载的最后一个文档的标记。
  3. 如果有更多记录 (20),则转到步骤 2。如果没有记录,则删除滚动。

类似这样的:

Request request = new Request("GET", "/" + index + "/_search?scroll=1m");
request.setEntity(new NStringEntity(jsonQuery, ContentType.APPLICATION_JSON));
Response response = client.performRequest(request);
String scrollId = jsonNode.path("_scroll_id").asText();

//Extract data you need

//Repeat the scroll, by passing scroll_id

Request scrollRequest = new Request("GET", "/_search/scroll");
scrollRequest.setEntity(new NStringEntity("{\"scroll_id\": \"" + scrollId + "\"}", ContentType.APPLICATION_JSON));
response = client.performRequest(scrollRequest);
responseBody = EntityUtils.toString(response.getEntity());
scrollId = jsonNode.path("_scroll_id").asText();
© www.soinside.com 2019 - 2024. All rights reserved.