使用此方法在 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 批次的代码中放置一个计数器可能会起作用,但我不确定如何做到这一点。
关于如何处理这个问题有什么建议吗?
要下载大量记录,您可以使用 Scroll API:
https://www.elastic.co/guide/en/elasticsearch/reference/current/scroll-api.html
机制非常简单:
scroll_id
,它是您下载的最后一个文档的标记。类似这样的:
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();